在树枝中显示嵌套数组

时间:2015-09-08 21:04:16

标签: php arrays symfony twig

这是我的Message实体。它是一个在我的应用程序中定义用户之间的消息的类。

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循环,我不知道如果这个格式的深度大于2,我不知道如何显示这种格式。

{% 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 %}

这将显示消息的每个回复,但如何以全深度显示嵌套消息?

2 个答案:

答案 0 :(得分:4)

我没有测试过你应该能够循环回复:

{% 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 %}

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

答案 1 :(得分:1)

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

在主枝中

,打印主消息,并按部分递归迭代:

## 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>

希望这个帮助

相关问题