遞歸性是一個神奇的詞根據東西的深度做循環任意數量。
在你的情況,你可以嘗試這樣的事:
<h3 class="titleComments">Comments</h3>
{# imports macros contained in this file inside "me" variable #}
{% import _self as me %}
{# makes the variable exists and loopable whatever the case #}
{% set replies = replies|default([]) %}
{% for comment in comments %}
<p><strong>{{ comment.author }}</strong> {{ comment.content }}</p>
<button type="button" class="btn-primary btn-xs buttonAnswer btnLevel0" value="{{ comment.id }}"> answer</button>
{# fixes the non-unique id issue in your current code #}
<div id="reply-{{ comment.id }}">
{% if replies|length %}
{% for reply in replies %}
{% if reply.getComParent() == comment.id and reply.level == 0 %}
<p> <strong>{{ reply.author }}</strong> {{ reply.content }}</p>
<button type="button" class="btn-primary btn-xs buttonAnswer btnLevel{{ level }}" value="{{ reply.id }}" data-level="{{ reply.level1 }}" > answer </button>
{# here begins the magiC#}
{{ me.displayReplies(replies, reply, 1) }}
{% endif %}
{% endfor %}
{% else %}
no comment(s).
{% endif %}
</div>
{% endfor %}
{# macros are similar to functions in php and can call themselves recursivly #}
{% macro displayReplies(replyParent, replies, level) %}
{# reimports macros to make this macro able to call himself #}
{% import _self as me %}
{% for replyChild in replies %}
<div id="underReply{{ replyParent.id }}">
{% if replyChild.getComParent() == replyParent.id and replyChild.level == level %}
<p> <strong>{{ replyChild.author }}</strong> {{ replyChild.content }}</p>
<button type="button" class="btn-primary btn-xs buttonAnswer btnLevel2" value="{{ replyChild.id }}" data-level="{{ replyChild.level2 }}" > answer </button>
{# and we get deeper if needed #}
{{ me.displayReplies(replyChild, replies, level + 1) }}
{% endif %}
</div>
{% endfor %}
{% endmacro %}
順便說一句,一個簡單的方法應該是使用相同的表有關評論和回覆:
考慮以下內容:
comments:
-
id: 1
parent_id: null
content: comment 1
-
id: 2
parent_id: null
content: comment 2
-
id: 3
parent_id: null
content: comment 3
-
id: 4
parent_id: 1
content: reply comment 1
-
id: 5
parent_id: 2
content: reply comment 2
-
id: 6
parent_id: 3
content: reply comment 3
-
id: 7
parent_id: 3
content: reply comment 3
-
id: 8
parent_id: 4
content: reply of reply comment 3
-
id: 9
parent_id: 8
content: reply of reply of reply comment 3
而下面的小枝代碼:
{% import _self as me %}
{% macro displayComment(comments, parentComment, deepness = 0) %}
{% if parentComment.parent_id is null %}
Comment #{{ parentComment.id }} (deepness = {{ deepness }}): {{ parentComment.content }}
{% else %}
{% for i in 0..deepness %} {% endfor %}Reply #{{ parentComment.id }} of #{{ parentComment.parent_id }} (deepness = {{ deepness }}): {{ parentComment.content }}
{% endif %}
{% import _self as me %}
{% for childComment in comments if childComment.parent_id == parentComment.id %}
{{ me.displayComment(comments, childComment, deepness + 1) }}
{% endfor %}
{% endmacro %}
{% for comment in comments if comment.parent_id is null %}
{{ me.displayComment(comments, comment) }}
{% endfor %}
您將結束:
Comment #1 (deepness = 0): comment 1
Reply #4 of #1 (deepness = 1): reply comment 1
Reply #8 of #4 (deepness = 2): reply of reply comment 3
Reply #9 of #8 (deepness = 3): reply of reply of reply comment 3
Comment #2 (deepness = 0): comment 2
Reply #5 of #2 (deepness = 1): reply comment 2
Comment #3 (deepness = 0): comment 3
Reply #6 of #3 (deepness = 1): reply comment 3
Reply #7 of #3 (deepness = 1): reply comment 3
See fiddle
你確定'reply_level'是一個整數? –
是的,這是一個整數 – chk35