2
マクロを使用してTWIGで再帰関数を作成しました。このマクロは、ネストされた配列内の記述を何回見つけることができるのかを数えて返します。TWIG:この再帰的反復で何が問題になるのですか?
誰かが間違っていることを解決するのに手伝ってもらえますか?それはあなたの時間のための6
感謝しなければなりませんしながら、あなたが見ることができるように結果が4あり
:フィドルはで見つけることができます!
よろしく、 ジャスパー
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'