Symfony2 Twig 打印变量相关实体属性

发布于 2024-12-31 23:08:26 字数 521 浏览 1 评论 0原文

我在尝试访问树枝中的变量时遇到一些麻烦。

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苹果你个爱泡泡 2025-01-07 23:08:26

我将在图像实体中定义 __toString 方法,如下所示:

public function __toString() {
返回 $this->getWebPath();
然后

您将访问 Twig 中的变量,如下所示:

image

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" />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文