2017-02-03 6 views
2

マクロを使用してTWIGで再帰関数を作成しました。このマクロは、ネストされた配列内の記述を何回見つけることができるのかを数えて返します。TWIG:この再帰的反復で何が問題になるのですか?

誰かが間違っていることを解決するのに手伝ってもらえますか?それはあなたの時間のための6

感謝しなければなりませんしながら、あなたが見ることができるように結果が4あり

https://twigfiddle.com/5uskoi

:フィドルはで見つけることができます!

よろしく、 ジャスパー

TWIGコード:

{% macro countArray(item) %} 
    {% import _self as self %} 

    {% set total = 1 %} 

    {% for yesItem in item.yes.items %} 
     {% set total = total + self.countArray(yesItem) %} 
    {% endfor %} 

    {% for noItem in item.no.items %} 
     {% set total = + total + self.countArray(noItem) %} 
    {% endfor %} 

    {{ total }} 

{% endmacro %} 

{% from _self import countArray %} 

{% for item in data.items %} 

    {{ countArray(item) }}  

{% endfor %} 

このデータで:

data: 
    items:      
     - description: '1' 
      yes: 
       items: 
        - description: '2' 
        - description: '3' 
      no: 
       items:      
        - description: '4' 
        yes: 
         items: 
          - description: '5' 
          - description: '6' 

答えて

0

は、この問題に対する解決策を見つけました。マクロ呼び出しの後に| trimを追加しました。これは、番号で返されたすべての空白やその他のものを整えます。

これにより、これらを一緒に追加するという問題が発生しました。

詳細については、twigfiddleがこの設定で更新されました。

よろしくお願いします。 ジャスパー

関連する問題