CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Successfully printing PDF mail merge letters with Smarty tokens
Pages: [1]

Author Topic: Successfully printing PDF mail merge letters with Smarty tokens  (Read 37759 times)

Denim

  • I’m new here
  • *
  • Posts: 4
  • Karma: 2
  • CiviCRM version: 4.5.0
  • CMS version: Drupal 7.23
  • MySQL version: 5.5.37
  • PHP version: 5.4.6
Successfully printing PDF mail merge letters with Smarty tokens
May 13, 2013, 11:07:15 am
After much trial and error, I was able to successfully use Smarty tokens to process basic if/else workflows in a PDF letter to contacts, using Civi 4.3.1 and Drupal 7.22.  Thought I'd post it here as it might help others (and me, when I have to come back to it).  The workflow was to include certain custom fields based on their content.  The PDF was constructed using the HTML interface.

 - The Smarty tokens (actually, Smarty variables) are prefixed with a $: {if $contact.first_name eq John}Hello John{/if}.  The CiviCRM tokens have no prefix: {contact.first_name}
 
- In order for a contact element like {$contact.first_name} to be visible to the Smarty template engine it had to first be included somewhere in the message body  as a regular Civi token: {contact.first_name}.    This is necessary because the Smarty contact array is built from the contact tokens found in the html form by CRM_Utils_Token::getTokenDetails (CRM/Contact/Form/Task/PDFLetterCommon.php around line 321).  If the Civi token wasn't needed as output in the letter body itself, it could be hidden inside a Smarty comment {* {contact.first_name} *}.

 - The Smarty contact array elements were being loaded with the contact_id as key: contact[201].first_name.  To avoid having to use an unknown contactID when referencing the Smarty contact elements, I changed line ~340 in  CRM/Contact/Form/Task/PDFLetterCommon.php from
  $smarty->assign_by_ref('contact', $contact);
to
  $smarty->assign_by_ref('contact', $contact[$contactId]);
Array elements were then referenced as expected, {$contact.first_name}.
Not a permanent solution, of course - a Civi update would overwrite the change. And, I didn't test for any side effects this might have.

 - String comparisons were hairy because of the HTML editor - it would replace quotes with the hex equivalent, causing Smarty to choke.  I found a workaround was to avoid quotes entirely: {if $contact.first_name eq John}.  It may be possible to assign a string to a Smarty variable, then do a string comparison on the variable: {assign var=foo value="Foo Bar"}, but I didn't test it.

krypto

  • I post occasionally
  • **
  • Posts: 69
  • Karma: 5
  • IT guy for @bhahumanists. Running Civi on WP.
    • British Humanist Association
  • CiviCRM version: 4.5.8
  • CMS version: WordPress 4.x
  • MySQL version: 5.5.x
  • PHP version: 5.5.x
Re: Successfully printing PDF mail merge letters with Smarty tokens
July 25, 2013, 10:24:46 am
Thanks for posting this!

On the last point, you can stop CKEditor converting the quotes by editing ckeditor.js and removing 'quot' from the list that includes 'iexcel,cent,pound' etc.

Edit: actually the above only works for double quotes, not the single ones. As Denim suggests, the best way around this is just not to use single quotes at all - seems to work ok.
« Last Edit: August 08, 2013, 03:51:13 am by krypto »

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Core CiviCRM Functions (Moderator: Yashodha Chaku) »
  • Successfully printing PDF mail merge letters with Smarty tokens

This forum was archived on 2017-11-26.