7

How I can use if/else conditions inside the email template in SugarCRM? I am trying use conditions equal pdf template and smarty template but I have no success.

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

ErasmoOliveira
  • 1,397
  • 2
  • 19
  • 39

3 Answers3

4

It seems, that official SugarCRM docs don't provide any information about using if/else conditions in email templates. I didn't believed them, so i dug into SugarCRM code.

Research:

Sending e-mail is done in EmailMan class in method sendEmail:

$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 parse_email_template method from class EmailTemplate. It's not so well-written, as i was thinking. And it's only providing basic variable replacing. Let's look at it 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;
    }

Conclusion:

What i can say more - SugarCRM at this point doesn't provide any conditions nor smarty or other template engine. You can try to modify their code to implement it, but i wouldn't recommend that as it is a litlle spaghetti ;)

Paweł Tomkiel
  • 1,848
  • 2
  • 19
  • 38
2

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

{{#if yourcondition}} action {{else}} action{{/if}}
mg33dev
  • 150
  • 1
  • 19
1

Try this and see how you go:

{if $fieldname!="value"}sometext {$fieldname} {/if}
Atomix
  • 12,019
  • 9
  • 36
  • 43
Luke
  • 11
  • 1