【问题标题】:Displaying nested array in twig在树枝中显示嵌套数组
【发布时间】:2015-09-08 21:04:15
【问题描述】:

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

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

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

【问题讨论】:

标签: php arrays symfony twig


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    您可以执行以下递归方法:

    在主树枝中,您打印主要消息,并在部分中递归迭代,如下所示:

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

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      • 2020-11-06
      • 2015-07-05
      相关资源
      最近更新 更多