题
我正在尝试使用嵌套如果我的电子邮件模板中的语句,如以下内容:
{{if subscriber.promo_group}}
<p>You are one of the first {{var subscriber.promo_group}} subscribers.</p>
{{/if}}
{{if subscriber.coupon_code}}
<p>Use code {{htmlescape var=$subscriber.coupon_code}} for {{htmlescape var=$subscriber.discount_amount}} off.</p>
{{if subscriber.partner_coupon_code}}
<p>Or, code {{var subscriber.partner_coupon_code}} for {{htmlescape var=$subscriber.partner_discount_amount}} off at checkout.</p>
{{/if}}
{{else}}
{{if subscriber.partner_coupon_code}}
<p>Use code {{htmlescape var=$subscriber.partner_coupon_code}} for {{htmlescape var=$subscriber.partner_discount_amount}} off at checkout.</p>
{{/if}}
{{/if}}
但是,当我收到电子邮件时,我会得到这样的信息:
You are one of the first 20 subscribers.
Use code XXXXX-QTEALK15 for $35 off.
Or, code XXXXX10OFF for 10% off at checkout.
{{else}}
Use code XXXXX10OFF for 10% off at checkout.
{{/if}}
如果Magento电子邮件模板中的语句,是否可以使用嵌套?
解决方案
如果您看一下开始的开始 Varien_Filter_Template
课程您会找到以下两个常数。
const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{\\/depend\s*}}/si';
const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si';
在正则表达 CONSTRUCTION_IF_PATTERN
您会注意到它具有
{{{如果条件}} text在这里{{else}}其他文本转到这里{{/if}}
所以,不幸的是筑巢 if
陈述是不可能的,因为第一个匹配 {{/if}}
将陷入正则表达式中。
虽然,班级提供了其他东西 {{if}}
陈述, {{depend}}
陈述。它几乎与 {{if}}
除了没有 {{else}}
功能。
幸运的是,在您的情况下,嵌套条件并不复杂,可以使用 {{depend}}
. 。因此,您可以有以下内容:
{{if subscriber.promo_group}}
<p>You are one of the first {{var subscriber.promo_group}} subscribers.</p>
{{/if}}
{{if subscriber.coupon_code}}
<p>Use code {{htmlescape var=$subscriber.coupon_code}} for {{htmlescape var=$subscriber.discount_amount}} off.</p>
{{depend subscriber.partner_coupon_code}}
<p>Or, code {{var subscriber.partner_coupon_code}} for {{htmlescape var=$subscriber.partner_discount_amount}} off at checkout.</p>
{{/depend}}
{{else}}
{{depend subscriber.partner_coupon_code}}
<p>Use code {{htmlescape var=$subscriber.partner_coupon_code}} for {{htmlescape var=$subscriber.partner_discount_amount}} off at checkout.</p>
{{/depend}}
{{/if}}
如果需要更复杂的是,最好只使用模板的块类简化逻辑。
其他提示
我从未尝试过这样的嵌套控制语句,但是一种可能的解决方案(尽管可能比您想要的要多一些)是包括标准 .phtml
电子邮件模板中的模板。
核心使用此功能,对于您需要在电子邮件模板内运行一些PHP的情况可能非常方便。
看一下:
app/locale/en_US/template/email/sales/order_new.html
也:
app/design/frontend/base/default/layout/sales.xml
在里面 order_new.html
模板,检查第97行(或其他),您将看到此调用:
{{layout handle="sales_email_order_items" order=$order}}
此呼叫检查您的布局文件的句柄 sales_email_order_items
. 。在 sales.xml
您会在第268行附近找到它。您会在模板中看到此负载 email/order/invoice/items.phtml
(除其他事项外)。
从这里开始,这都是非常标准的洋红色布局东西。如果您看着 items.phtml
模板,您会注意到的第一件事是它分配 $_order
多变的。这是在电子邮件模板的布局句柄中通过的 order=$order
. 。一旦你进入 items.phtml
, ,他们只是使用 $this->getOrder()
.