title: Displaying nested array in twig
body: <p>Here is my Message entity. It is a class that defines messages between users in my app.</p>
  
  <pre><code>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-&gt;replies = new ArrayCollection();
      }
  </code></pre>
  
  <p>What's important to note is the <code>replyof</code> variable, which tells what message is a parent of the message. If it's NULL, then the message is not a reply, it's a parent message(a root). </p>
  
  <p>And the <code>messages</code> variable, which is an array of Messages that are replies to the message. These replies can have replies, themselves. This array can also be NULL, for leaf nodes, because they don't have any reply.</p>
  
  <p>All the other variables contain just some fields that define an actual message between two users.</p>
  
  <p>What I am trying to do is display in Twig all of my messages in an arborescent format, like so:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The problem is that Twig only supports <code>foreach</code> loops and I'm not sure how to display this format when it has a higher depth, bigger than two.</p>
  
  <pre><code>{% for reply in message.replies %}
      &lt;li&gt; sent by: {{ reply.sender }} &lt;/li&gt;
      &lt;li&gt; title: {{ reply.title }} &lt;/li&gt;
      &lt;li&gt; content: {{ reply.content }} &lt;/li&gt;
      &lt;li&gt; date: {{ reply.sentAt|date('d-m-Y H:i:s') }} &lt;/li&gt;
      &lt;hr&gt;
  {% endfor %}
  </code></pre>
  
  <p>This will display every reply of a message, but how can I display nested messages, in full depth? </p>