Shawn Northrop
Shawn Northrop

Reputation: 6016

Symfony2 Twig printing variable related entity properties

I am having some trouble trying to access variables in twig.

I have a class called staff and a class called image

every staff has 1 image_id which maps to the image table

I can call the following code in php to access the image url

//...get 1 staff member
echo $staff->getImage()->getWebPath();

However calling this code in twig does not seem to work

{{ staff.image.webpath }}

If I pull the image in php and pass it to the template i can access it like so

php
---
$image = $staff->getImage();

twig
----
{{ image.webpath }}

I would like to pass all staff to my template and then use a for loop to print out their names bios titles and images. Is this possible to do with the Image?

Upvotes: 1

Views: 1898

Answers (1)

Max Małecki
Max Małecki

Reputation: 1702

I will define the __toString method in the image entity like this:

public function __toString() { return $this->getWebPath(); }

Then you will access variable in the Twig like:

<img src="{{ staff.image }}" alt="image" />

Upvotes: 3

Related Questions