Symfony2 Twig 打印变量相关实体属性
我在尝试访问树枝中的变量时遇到一些麻烦。
我有一个名为 Staff 的类和一个名为 image 的类,
每个工作人员都有 1 个 image_id,它映射到图像表
我可以在 php 中调用以下代码来访问图像 url
//...get 1 staff member
echo $staff->getImage()->getWebPath();
但是在 twig 中调用此代码似乎不起作用
{{ staff.image.webpath }}
如果我拉在 php 中创建图像并将其传递到模板我可以像这样访问它,所以
php
---
$image = $staff->getImage();
twig
----
{{ image.webpath }}
我想将所有员工传递到我的模板,然后使用 for 循环打印出他们的姓名、BIOS 标题和图像。这可能与图像有关吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将在图像实体中定义 __toString 方法,如下所示:
public function __toString() {
返回 $this->getWebPath();
然后
您将访问 Twig 中的变量,如下所示:
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" />