2017-10-09 10 views
0

루프 내의 값에 따라 루프 내에서 다른 값을 지정하려고합니다. has_variants 속성이 나는 변종 반복, 그렇지 않으면 재산 master.id를 사용하려면 false 경우Dust.js가있는 루프 내에서/else else

{ 
    "products":[ 
    { 
     "id":1, 
     "name":"Ruby on Rails Tote", 
     "has_variants":false, 
     "master":{ 
     "id":1, 
     "name":"Ruby on Rails Tote", 
     "sku":"ROR-00011", 
     "price":"15.99", 
     "weight":"0.0", 
     "height":null, 
     "width":null, 
     "depth":null, 
     "is_master":true, 
     "slug":"ruby-on-rails-tote", 
     "description":"Autem ducimus ipsum temporibus doloremque voluptatem eos ea. Et esse nobis consequatur quam tempora quia assumenda. Ipsam magni quaerat ipsa ut commodi et quae. Eligendi rerum in ratione id et nemo sunt. Atque dolore voluptatem quis accusantium id.", 
     "track_inventory":true, 
     "cost_price":"17.0", 
     "option_values":[ 

     ], 
     "display_price":"$15.99", 
     "options_text":"", 
     "in_stock":true, 
     "is_backorderable":true, 
     "total_on_hand":50, 
     "is_destroyed":false 
     }, 
     "variants":[ 

     ], 

     ... 

(이 제품에 여러 수), 그리고 id 속성을 사용하여 다음 입력하여

각 변형의 단지 변형 식별자를 출력하고 마스터 ID를 생략

<outer>{~n} 
{#products} 
{@eq key=has_variants value=true} 
The ID is {master.id}{~n} 
{:else} 
{#variants} 
The ID is {id}{~n} 
{/variants} 
{/eq} 
{/products} 
</outer> 

:

나는 다음 시도했다.

목록을 반복하고 속성 값에 따라 조건부 사용 속성을 사용하려면 어떻게해야합니까?

답변

1

거의 완벽합니다. 대신 eq을 사용하는 대신 속성을 직접 사용하십시오. eq은 느슨한 비교입니다.

<outer>{~n} 
    {#products} 
    {#has_variants} 
     The ID is {master.id}{~n} 
    {:else} 
     {#variants} 
     The ID is {id}{~n} 
     {/variants} 
    {/has_variants} 
    {/products} 
</outer> 
+0

정말로 'eq'를 사용하고 싶다면 모든 더스트 도우미는 참조, 문자열 및 숫자 인수 만 취합니다. 그러므로 true와 비교하고 싶다면'value '와 비교자를 문자열에서 bool로 강제 변환하기 위해'{@eq key = has_variants value = "true"type = "boolean"}'을 사용하십시오. – Interrobang