2016-09-25 10 views
1

商品の種類に基づいて商品ページにスニペットとテンプレートを含めるようにしています。しかし、液体は条件付きでスニペットを生成するようには見えません。液体入りのスニペットとテンプレートを含む

私が達成しようとしているものの例:

{% if product.type == 'shoes' %} 
    {% include 'shoes-template' %} 
{% else %} 
    {% include 'other-template' %} 
{% endif %} 
+0

何かあります。条件付きの 'include'が動作します。私は多くの場所でそれを使用しています。他の関連コードを共有することはできますか? 'shoes-template'と' other-template'も同じですか? – HymnZ

答えて

2

あなたは多くの製品の種類を持っている場合は、代わりに複数のifelse ifを使用しての、あなたがアレイとcontainsを使用することができます。 captureを実行し、文字列 "Liquid error"を検索して、テンプレートが存在するかどうかを確認することもできます。

{% assign types = "shoes, shirts, pants" | split:", " %} 
{% if types contains product.type %} 
    {% assign snip = product.type | append:"-template" %} 
{% else %} 
    {% assign snip = "other-template" %} 
{% endif %} 

{% capture snip_content %}{% include snip %}{% endcapture %} 
{% unless snip_content contains "Liquid error" %} 
    {% include snip %} 
{% endunless %} 
+0

これは方法についてのラウンドです。それが効率的かどうかは分かりません。また、 '' "shoes"、 "shirts"] 'は液体コードの配列を意味しません。 – HymnZ

+1

私は配列を修正しました。多くの製品タイプがある場合、テンプレートファイル名と一貫している限り、長い '' if else else if else ... ''より効率的です。 – jrbedard

関連する問題