2017-04-12 92 views
0

我想在handlebars中使用查找功能來傳遞一個動態的部分,但它不是很適合我。如何將變量傳遞給句柄中的一個部分?

我的數據是:

{ 
"sections": [ 
    "about", 
    "when", 
    "where" 
]} 

模板的樣子:

{{#each copy.sections}} 
     <section class='section' id='section-{{this}}' aria-labelledby='hed-{{this}}'> 
      <h2 class='section__hed' id='hed-{{this}}'>{{this}}</h2> 
      {{> graphic/(lookup . 'this') }} // Need to render this dynamically 
     </section> 
    {{/each}} 

基本上我想查找解決爲: {{> graphic/about}}{{> graphic/when}},並{{> graphic/where}}

回答

0

如果你的數據爲:

var copy = { 
    "sections": [ 
    "about", 
    "when", 
    "where" 
]}; 

你只需要指定{{#each sections }}而不是{{#each copy.sections}}

{{#each sections}} 
    <section class='section' id='section-{{this}}' aria-labelledby='hed-{{this}}'> 
     <h2 class='section__hed' id='hed-{{this}}'>{{this}}</h2> 
     graph/{{ this }} 
    </section> 
{{/each}} 

我不能弄明白你的代碼{{> graphic/(lookup . 'this') }},它並沒有一個有效的語法,所以我假設你正在嘗試做的是graph/{{ this }}

+0

嗯我不認爲這正是我正在尋找。我必須使用'copy.sections',因爲它來自JSON文件,我使用'gulp-hb'來提取數據(我需要指定它來自哪個文件,因此是複製部分)。我正在嘗試使用動態部分。所以我需要它來解決,因爲它是自己的部分。例如,當它循環遍歷每個數組並且碰到數組中的第一個元素時,它將是'{{> graphic/about}}'並且拉入該部分。 – user5067101

+0

然後編輯您的帖子,並提供更完整的數據結構和更清晰的問題。我的回答是基於你對數據變量名稱的猜測。 – hcheung

+0

還有一件事,如果您正在閱讀json文件,那麼您的json文件名是什麼?如果你的json文件被稱爲mydata.json,那麼你可以用'{mydata mydata.sections}}'來獲得你想要的語法。這是我可以從你提供的內容中理解的部分,因爲你的數據結構沒有任何與'graphic'相關的東西。 – hcheung