在树枝中显示嵌套数组

Displaying nested array in twig

这是我的消息实体。它是一个 class,用于定义我的应用程序中用户之间的消息。

class Message
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @Assert\NotBlank(message="private_message.title.blank")
     * @ORM\Column(name="title", type="string", length=50)
     */
    protected $title;

    /**
     * @Assert\NotBlank(message="private_message.receiver.blank")
     * @ORM\ManyToOne(targetEntity="MedAppBundle\Entity\User")
     * @ORM\JoinColumn(referencedColumnName="id")
     */
    protected $receiver;
    /**
     * @ORM\ManyToOne(targetEntity="MedAppBundle\Entity\User")
     * @ORM\JoinColumn(referencedColumnName="id")
     */
    protected $sender;

    /**
     * @var string
     * @Assert\NotBlank(message="private_message.content.blank")
     * @ORM\Column(name="content", type="string")
     */
    protected $content;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="sentAt", type="datetime")
     */
    protected $sentAt;


    /**
     * @var boolean
     *
     * @ORM\Column(name="isSpam", type="boolean")
     */
    protected $isSpam = false;


    /**
     * @var \DateTime
     *
     * @ORM\Column(name="seenAt", type="datetime",nullable=true)
     */
    protected $seenAt = null;

    /**
     * @ORM\ManyToOne(targetEntity="PrivateMessageBundle\Entity\Message")
     * @ORM\JoinColumn(referencedColumnName="id",nullable=true)
     */
    protected $replyof;

    /**
     * @ORM\OneToMany(targetEntity="PrivateMessageBundle\Entity\Message", mappedBy="replyof")
     **/
    private $replies;

    public function __construct() {
        $this->replies = new ArrayCollection();
    }

需要注意的重要一点是 replyof 变量,它表明哪个消息是消息的父级。如果它是 NULL,则消息不是回复,它是父消息(根)。

messages 变量,这是一个消息数组,是对消息的回复。这些回复本身可以有回复。这个数组也可以为NULL,对于叶节点,因为它们没有任何回复。

所有其他变量只包含一些定义两个用户之间的实际消息的字段。

我想做的是以树形格式在 Twig 中显示我的所有消息,如下所示:

message1 - root message, reply of none, but has replies
   reply1 - first reply of message 1
      reply1 first reply of reply 1 of message 1, leaf with no further replies
   reply2 - second reply of message 1, leaf with no further replies

message2 - root message, no replies and a reply of none

问题是 Twig 只支持 foreach 循环,我不确定当它有更高的深度时如何显示这种格式,大于两个。

{% for reply in message.replies %}
    <li> sent by: {{ reply.sender }} </li>
    <li> title: {{ reply.title }} </li>
    <li> content: {{ reply.content }} </li>
    <li> date: {{ reply.sentAt|date('d-m-Y H:i:s') }} </li>
    <hr>
{% endfor %}

这将显示消息的每个回复,但我如何才能完整显示嵌套的消息?

您可以按如下方式执行递归方法:

在 main twig 中,打印主要消息,并在部分中递归迭代如下:

## main twig
Root message:
   <ul>
    <li> sent by: {{ message.sender }} </li>
    <li> title: {{ message.title }} </li>
    <li> content: {{ message.content }} </li>
    <li> date: {{ message.sentAt|date('d-m-Y H:i:s') }} </li>
    {{ include('AcmeDemoBundle:Message:_elem.html.twig', {'replies': message.replies ) }}
  </ul>

## AcmeDemoBundle:Message:_elem.html.twig
<ul>
{% for reply in replies %}
    <li> sent by: {{ reply.sender }} </li>
    <li> title: {{ reply.title }} </li>
    <li> content: {{ reply.content }} </li>
    <li> date: {{ reply.sentAt|date('d-m-Y H:i:s') }} </li>
    {{ include('AcmeDemoBundle:Message:_elem.html.twig', {'replies': reply.replies ) }}
{% endfor %}
</ul>

希望对您有所帮助

我没有测试过,你应该可以循环回复:

{% for reply in message.replies %}
    {% if loop.first %}<ul>{% endif %}
    <li> sent by: {{ reply.sender }} </li>
    <li> title: {{ reply.title }} </li>
    <li> content: {{ reply.content }} </li>
    <li> date: {{ reply.sentAt|date('d-m-Y H:i:s') }} </li>
    {% for reply in reply.replies %}
        {% if loop.first %}<li><ul>{% endif %}
        <li> sent by: {{ reply.sender }} </li>
        <li> title: {{ reply.title }} </li>
        <li> content: {{ reply.content }} </li>
        <li> date: {{ reply.sentAt|date('d-m-Y H:i:s') }} </li>
        {% if loop.last %}</ul></li>{% endif %}
    {% endfor %}
    {% if loop.last %}</ul>{% endif %}
{% endfor %}

只会显示2级回复。您可以使用 Twig macro 来定义应该递归显示回复的可重用函数:

{# define the macro #}
{% macro displayReply(reply) %}
    <li> sent by: {{ reply.sender }} </li>
    <li> title: {{ reply.title }} </li>
    <li> content: {{ reply.content }} </li>
    <li> date: {{ reply.sentAt|date('d-m-Y H:i:s') }} </li>
    {% for reply in reply.replies %}
        {% if loop.first %}<li><ul>{% endif %}
        {{ displayReply(reply) }}
        {% if loop.last %}</ul></li>{% endif %}
    {% endfor %}
{% endmacro %}

{# use the macro #}
{% for reply in message.replies %}
    {% if loop.first %}<ul>{% endif %}
    {{ displayReply(reply) }}
    {% if loop.last %}</ul>{% endif %}
{% endfor %}

根据您的查询,它可能会以错误的顺序显示回复,您可能需要在查询中按降序对回复进行排序。