H
H
hitakiri2016-02-19 14:03:37
symfony
hitakiri, 2016-02-19 14:03:37

How to display data in Twig template engine?

Good time. I ran into an unexpected problem while learning Symfony. The Twig template engine throws the following error:

Method "..." for object "..." does not exist in ... at line

the template itself:
{% for all in books %}
<li>{{ all.idBook }}</li>
{% endfor %}

controller:
public function BookAction()
    {
        $repository = $this->getDoctrine()->getRepository('BookBundle:Book');

        if (!$repository) {
        throw $this->createNotFoundException('No book found');
        }
        
        $books = $repository->findAll();
        
        $booksContent = $this->render('BookBundle:Default:book.html.twig', array('books' => $books));
        return new Response($booksContent);
    }

mapping:
/**
     * @var int
     *
     * @ORM\Column(name="id_book", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id_book;

   public function getBookId()                 {return $this->id_book;}

while the template {{ dump(books) }} produces:
array:3 [▼
  0 => Book {#376 ▼
    #id_book: 1
    ...

Works on Symfony 3.0.2 and Twig 1.24 , config.yml - didn't change anything, no additional bundles.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
BoShurik, 2016-02-19
@hitakiri

You have the id_book variable and the getBookId getter.

A
Alexander, 2016-02-19
@ghost1k

all.idBook
protected $id_book;
something doesn't fit here

A
Alexey Romanenko, 2016-02-19
@slimus

do you have a getIdBook() method in entity?
According to your data, the whole picture is not visible, it can be anything

R
Ruslan Kvashnin, 2016-02-19
@RussellKvashnin

Yes, here the author knows something, you yourself know what ...

if (!$repository) {
     throw $this->createNotFoundException('No book found');
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question