電子メールテンプレートのステートメントの場合はネストされています
-
16-10-2019 - |
質問
以下のように、私のメールテンプレートにstatementsのネストされたものを使用しようとしています。
{{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の電子メールテンプレートでネストされたIFステートメントを使用することは可能ですか?
解決
の開始を見ると Varien_Filter_Template
クラス次の2つの定数があります。
const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{\\/depend\s*}}/si';
const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si';
の正規表現 CONSTRUCTION_IF_PATTERN
あなたはそれが形を持っていることに気付くでしょう
{{条件}}テキストがここに行く{{{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}}
これをより複雑にする必要がある場合は、テンプレートのブロッククラスを使用してロジックを簡素化することをお勧めします。
他のヒント
私はそのようなネストコントロールステートメントを試したことはありませんが、考えられる解決策の1つは(あなたが望んでいるよりも少し多くの作業かもしれませんが)標準を含めることです .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
(とりわけ)。
これはすべてここからかなり標準的なMagentoレイアウトのものです。をご覧ください items.phtml
テンプレート、最初に気付くのは、 $_order
変数。これは、電子メールテンプレートのレイアウトハンドルで渡されています order=$order
. 。入ると items.phtml
, 、彼らはその変数を使用して割り当てているだけです $this->getOrder()
.