twig, how to convert array in the object to string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gintare
    New Member
    • Mar 2007
    • 103

    twig, how to convert array in the object to string?

    Related files:

    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;
        } 
    }
    myproject\src\B log\ModelBundle \Entity\Comment .php
    Code:
    class Comment {
    ...
    private $body;
        public function getBody()
        {
            return $this->body;
        }
    }
    myproject\src\B log\AdminBundle \Resources\view s\Post\index.ht ml.twig

    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!
  • gintare
    New Member
    • Mar 2007
    • 103

    #2
    One of solutions seems to be


    ~: Converts all operands into strings and concatenates them. {{ "Hello " ~ name ~ "!" }} would return (assuming name is 'John') Hello John!.

    but if i try:
    <td>{{ ~ entity.author ~ }}</td>
    it gives an error:
    "Unexpected token "operator" of value "~" in src/Blog/AdminBundle/Resources/views/Post/index.html.twig at line 35 "

    Comment

    • gintare
      New Member
      • Mar 2007
      • 103

      #3
      The error was that i should use method __toString() instead of toString() in \myproject\src\ Blog\ModelBundl e\Entity\Author .php

      Comment

      Working...