Magento电子邮件模板如果语句 [英] Magento Email Template If Statements

查看:97
本文介绍了Magento电子邮件模板如果语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Magento电子邮件模板如果语句在我预期的时候没有评估为true。有人可以告诉我有什么问题吗?看看下面的代码:

  {{var customer.group_id}} 
{{if customer.group_id }}打印true {{else}}打印false {{/ if}}
{{if customer.group_id == 4}}打印true {{else}}打印false {{/ if}}
{{if customer.group_id = 4}}打印true {{else}}打印false {{/ if}}
{{if customer.group_id eq 4}}打印true {{else}}打印false { {/ if}}

输出是

  4 
打印True
打印False
打印False
打印False

我试着把引号放在4左右,但结果相同。如何评估与magento电子邮件模板if的平等?

解决方案

挖掘代码,看起来模板逻辑被实现 Varien_Filter_Template (在lib \Varien不是app \code下)在过滤器函数中发出回调给如果模式与正则表达式匹配,则 ifDirective 函数。 ifDirective 反过来使用 _getVariable 函数来评估您的如果条件。 _getVariable 然后将 Varien_Filter_Template_Tokenizer_Variable 中的条件标记为属性或方法。

  if($ this-> isWhiteSpace()){
//忽略空格
继续;
} else if($ this-> char()!='。&& $ this-> char()!='('){
//属性或方法名X- 20045454545×20045 X-454545 X-454545 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- $ b $ methodArgs = $ this-> getMethodArgs();
$ actions [] = array('type'=>'method',
'name'=> $ parameterName,
'args'=> $ methodArgs);
$ parameterName ='';
} else if($ parameterName!=''){
//属性或变量声明
if($ variableSet){
$ actions [] = array('type'=>'property',
'name'=> $ parameterName);
} else {
$ variableSet = true;
$ actions [] = array('type'=>'variable',
名称 => $ parameterName)以;
}
$ parameterName ='';
}

当if条件被检测为一个方法时,它将执行该方法,否则它只是返回变量的字符串值。



所有这些都意味着(我想!)如果要评估if语句中的表达式,则需要添加一个新的customer属性(有可用的扩展名为此)模板可以评估。所以如果你定义一个布尔isMemberOfGroupNameX属性,那么模板应该可以工作。新评新新新旗新新新旗新新旗旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗旗新新旗旗新1992新新新新旗新新旗新200新新旗新新旗新新旗新新旗新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗旗哨旗新新新新新旗新新旗新旗新旗新旗新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新旗旗新旗新旗新旗新旗新新旗新新旗新旗旗旗新旗b

HTH,
JD


The Magento Email Template If Statements aren't evaluating to true when I expect them to. Can someone tell me what's wrong? Take a look at the following code:

{{var customer.group_id}}
{{if customer.group_id}}Print true{{else}}Print false{{/if}}
{{if customer.group_id==4}}Print true{{else}}Print false{{/if}}
{{if customer.group_id=4}}Print true{{else}}Print false{{/if}}
{{if customer.group_id eq 4}}Print true{{else}}Print false{{/if}}

The output is

4
Print True
Print False
Print False
Print False

I tried putting quotes around the 4, but same result. How do I evaluate equalities with magento email template if statements?

解决方案

Digging through the code, it looks like the template logic is implemented by Varien_Filter_Template (under lib\Varien not app\code) in the filter function which issues a callback to the ifDirective function if the pattern matches the regex. The ifDirective in turn uses the _getVariable function to evaluate your if condition. _getVariable then tokenizes the condition in Varien_Filter_Template_Tokenizer_Variable into either a property or a method.

if($this->isWhiteSpace()) {
            // Ignore white spaces
            continue;
        } else if($this->char()!='.' && $this->char()!='(') {
            // Property or method name
            $parameterName .= $this->char();
        } else if($this->char()=='(') {
            // Method declaration
            $methodArgs = $this->getMethodArgs();
            $actions[] = array('type'=>'method',
                               'name'=>$parameterName,
                               'args'=>$methodArgs);
            $parameterName = '';
        } else if($parameterName!='') {
            // Property or variable declaration
            if($variableSet) {
                $actions[] = array('type'=>'property',
                                   'name'=>$parameterName);
            } else {
                $variableSet = true;
                $actions[] = array('type'=>'variable',
                                   'name'=>$parameterName);
            }
            $parameterName = '';
        }

When the if condition is detected to be a method, it will execute that method, otherwise it simply returns the string value of the variable.

All of which means (I think!) that if you want to evaluate an expression inside the if statement, you need to add a new customer attribute (there are extensions available for this) that the template can evaluate. So if you define a boolean "isMemberOfGroupNameX" attribute, then the template should work.

I imagine this is not the answer that you're looking for, but I'm fairly sure that's the case.

HTH, JD

这篇关于Magento电子邮件模板如果语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆