Magento 电子邮件模板 If 语句 [英] Magento Email Template If Statements
问题描述
Magento 电子邮件模板 If Statements 在我期望的时候没有评估为 true.有人可以告诉我出了什么问题吗?看看下面的代码:
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}}
输出是
4
Print True
Print False
Print False
Print False
我尝试在 4 周围加上引号,但结果相同.如何使用 magento 电子邮件模板 if 语句评估相等性?
I tried putting quotes around the 4, but same result. How do I evaluate equalities with magento email template if statements?
推荐答案
翻开代码,看起来模板逻辑是由Varien_Filter_Template
(在libVarien not appcode下)实现的filter
函数,如果模式与正则表达式匹配,它会向 ifDirective
函数发出回调.ifDirective
反过来使用 _getVariable
函数来评估您的 if
条件._getVariable
然后将 Varien_Filter_Template_Tokenizer_Variable
中的条件标记为属性或方法.
Digging through the code, it looks like the template logic is implemented by Varien_Filter_Template
(under libVarien not appcode) 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 = '';
}
当检测到if条件是一个方法时,它会执行那个方法,否则它只是简单地返回变量的字符串值.
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.
所有这些都意味着(我认为!)如果您想评估 if 语句中的表达式,您需要添加模板可以评估的新客户属性(有可用的扩展).因此,如果您定义了一个布尔值isMemberOfGroupNameX"属性,那么模板应该可以工作.
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,京东
这篇关于Magento 电子邮件模板 If 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!