Related files:
myproject\src\B log\ModelBundle \Entity\Post.ph p
myproject\src\B log\ModelBundle \Entity\Author. php
myproject\src\B log\ModelBundle \Entity\Comment .php
myproject\src\B log\AdminBundle \Resources\view s\Post\index.ht ml.twig
I have Post entity in Symfony projetc and would like to display it in index.html.twig .
I do not understand how to display Post object variables in twig template.
I am getting error "Twig_Error_Run time: An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Proxies\__CG__\ Blog\ModelBundl e\Entity\Author could not be converted to string") in src/Blog/AdminBundle/Resources/views/Post/index.html.twig at line 36."
Question is how to display Post object variables in twig template?
Thank you a lot!
myproject\src\B log\ModelBundle \Entity\Post.ph p
Code:
class Post{
private $id;
private $title;
private $slug;
private $body;
private $author;
private $comments;
public function getId()
{
return $this->id;
}
public function setTitle($title)
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->title;
}
public function setBody($body)
{
$this->body = $body;
return $this;
}
public function getBody()
{
return $this->body;
}
public function setAuthor(\Blog\ModelBundle\Entity\Author $author)
{
$this->author = $author;
return $this;
}
public function getAuthor()
{
return $this->author;
}
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
public function getSlug()
{
return $this->slug;
}
public function __construct()
{
$this->comments = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addComment(\Blog\ModelBundle\Entity\Comment $comments)
{
$this->comments[] = $comments;
return $this;
}
public function removeComment(\Blog\ModelBundle\Entity\Comment $comments)
{
$this->comments->removeElement($comments);
}
public function getComments()
{
return $this->comments;
}
public function toString()
{
$str = implode('|',$this->comments);
return $str;
}
}
myproject\src\B log\ModelBundle \Entity\Author. php
Code:
class Author {
...
private $name;
...
public function toString()
{
return $this->name;
}
}
Code:
class Comment {
...
private $body;
public function getBody()
{
return $this->body;
}
}
Code:
{% block section %}
<h1>Post list</h1>
<table class="records_list">
<thead>
<tr>
<th>Title</th>
<th>Author</th>
<th>Comments</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td>{{ entity.title }}</td>
<td>{{ entity.author }}</td>
<td>
<table class="comments_list">
{% for item in entity.comments %}
<tr>
<td> {{ item.body }}</td>
<td><a href="{{ path('blog_admin_comment_show', { 'id': item.id }) }}">show</a></td>
</tr>
{% endfor %}
</table>
</td>
{% endfor %}
</tbody>
</table>
{% endblock %}
I have Post entity in Symfony projetc and would like to display it in index.html.twig .
I do not understand how to display Post object variables in twig template.
I am getting error "Twig_Error_Run time: An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Proxies\__CG__\ Blog\ModelBundl e\Entity\Author could not be converted to string") in src/Blog/AdminBundle/Resources/views/Post/index.html.twig at line 36."
Question is how to display Post object variables in twig template?
Thank you a lot!
Comment