2017-07-29 98 views
0

我正在使用Twig PatternLab。JSON沒有替換變量

我得到了一個小問題與JSON和一個樹枝for循環。

凌00-H3-black.twig:

<h3 class="A-ChevronBlack"><a href="">{{ text.chevron }}</a></h3> 

分子00 mastertile.twig:

<div class="M-DMasterTile"> 
     <div class="image"></div> 
     <div class="content"> 
     {% include "atoms-h3-black" %} 
     </div> 
    </div> 

生物00 default2.twig:

{% for post in default2 %} 
      {% include "molecules-mastertile" %} 
    {% endfor %} 

和JSON inside Organism folder 00-default2.json

{ 
     "default2" : [ 
     { 
      "text" : { 
      "chevron" : "How to build a campfire", 
      "body" : "This is the body copy" 
      } 
     }, 
     { 
      "text" : { 
      "chevron":"Lorem Ipsum", 
      "body" : "This is the body copy" 
      } 
     } 
     ] 
    } 

我的期望是「default2」循環兩次,因爲我有一個JSON中有兩個項目的數組,並推送JSON內容。如果我從JSON數組中取出變量,它會顯示更改(但顯然是重複的)。

我在這裏做錯了什麼?

我感謝你的幫助

回答

1

include採用全球範圍內並沒有在它沒有變量text。使用include with語法將變量傳遞到內部作用域。

Organisms 00-default2.twig應該是這樣的:

{% for post in default2 %} 
     {% include "molecules-mastertile" with {'text': post.text} %} 
{% endfor %}