How to use conditions inside an email template in SugarCRM

How can I use if / else conditions inside an email template in SugarCRM? I am trying to use conditions equal to the pdf template and smarty template, but I have not succeeded.

No success

<?php if ({::past::Opportunities::name::} != {::future::Opportunities::name::}){ ?> 

No success

 {if {::past::Opportunities::name::} neq {::future::Opportunities::name::}} 

no success

 <!-- {if {::past::Opportunities::name::} neq {::future::Opportunities::name::}} --> 

Any success (?)

 ?????? 

thanks

+6
source share
3 answers

SugarCRM white papers do not seem to provide any information about using if / else clauses in email templates. I did not believe them, so I got into SugarCRM code.

Study:

Email is sent in the EmailMan class in the sendEmail method:

 $template_data = $this->current_emailtemplate ->parse_email_template( array( 'subject' => $this->current_emailtemplate->subject, 'body_html' => $this->current_emailtemplate->body_html, 'body' => $this->current_emailtemplate->body, ) , $focus_name, $module , $macro_nv); 

It uses the parse_email_template method of the EmailTemplate class . It was not as well written as I thought. And this only provides a replacement for the main variable. Let's look at this a little closer:

 function parse_email_template($template_text_array, $focus_name, $focus, &$macro_nv) { [...] //variable initiation //preparing prefixes to search for variables (all variables are in "$some_name" format $pattern_prefix = '$' . strtolower($beanList[$focus_name]) . '_'; $pattern_prefix_length = strlen($pattern_prefix); $pattern = '/\\' . $pattern_prefix . '[A-Za-z_0-9]*/'; foreach ($template_text_array as $key => $template_text) { [...] //searching for variables matching $pattern and replacing them with proper values $return_array[$key] = $template_text; } return $return_array; } 

Output:

What can I say more - SugarCRM currently does not provide any conditions, not smarty or another template engine. You can try changing your code to implement it, but I would not recommend this, since it is litlle spaghetti ;)

+3
source

can handlebarsjs help?
http://handlebarsjs.com/builtin_helpers.html

 {{#if yourcondition}} action {{else}} action{{/if}} 
+2
source

Try this and see how you go:

 {if $fieldname!="value"}sometext {$fieldname} {/if} 
+1
source

Source: https://habr.com/ru/post/982752/


All Articles