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

3 Answers 3

4
+50

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 ;)

1

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

{{#if yourcondition}} action {{else}} action{{/if}}
2
  • In monday I will try your solution :) Feb 28, 2015 at 1:05
  • @mg33dev - i dug up in code, i'm afraid that's not handlebars either. It's some "let's-preg-match-everything" attempt :( Mar 1, 2015 at 12:00
1

Try this and see how you go:

{if $fieldname!="value"}sometext {$fieldname} {/if}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.