递归性是一个神奇的词根据东西的深度做循环任意数量。
在你的情况,你可以尝试这样的事:
<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