Symfony2 Assetic 动态图像包含在 Twig 中

发布于 2025-01-05 05:13:28 字数 917 浏览 5 评论 0原文

我想使用 Twig 和 Assetic 加载文章的特定缩略图。这是不起作用的代码:

在配置和参数中:

thumbnail_dir="/Resources/public/images/article_thumbs"

twig:
    debug:             %kernel.debug%
    strict_variables: %kernel.debug%
    globals:
        thumbnail_dir: %thumbnail_dir%

在树枝文件中:

{% image '@MyMainBundle' ~ thumbnail_dir ~ '/' ~ id ~ '/' ~ article.thumbnailFile %}
    <img src="{{ asset_url }}"
         alt="{{ article.title }}" />
{% endimage %}

所以基本上我有图像存储在 /Resources/public/images/article_thumbs/{ID}/{FILE.ext}

我想要获取 Twig 中特定文章的这些内容。我怎样才能做到这一点?

编辑: 为什么我不把它们放在 web 文件夹中?

我想我曾经在某个时候考虑过使用 Assetic 过滤器,但我认为确实没有必要。

我暂时将它们存储在 web 文件夹中。

因此,如果我制作 thumbnail_dir="/images/article_thumbs" 并将其放入网络目录中,我只需说 {{ asset(thumbnail_dir ~ '/' ~ id ~ '/' ~ Article.thumbnailFile ) }} 在树枝中?

I want to load a specific thumbnail for an article using Twig and Assetic. Here is the code that doesn't work:

in config and params:

thumbnail_dir="/Resources/public/images/article_thumbs"

twig:
    debug:             %kernel.debug%
    strict_variables: %kernel.debug%
    globals:
        thumbnail_dir: %thumbnail_dir%

In the twig file:

{% image '@MyMainBundle' ~ thumbnail_dir ~ '/' ~ id ~ '/' ~ article.thumbnailFile %}
    <img src="{{ asset_url }}"
         alt="{{ article.title }}" />
{% endimage %}

So basically I have images I'm storing in /Resources/public/images/article_thumbs/{ID}/{FILE.ext}

I want to get those for a specific article in Twig. How can I achieve that?

EDIT:
Why am I not putting them in the web folder?

I guess I had thought about using Assetic filters at some point, but I suppose there is really no need.

I'll just store them in the web folder for now.

So if I make thumbnail_dir="/images/article_thumbs" and put that in the web dir, Do I just say {{ asset( thumbnail_dir ~ '/' ~ id ~ '/' ~ article.thumbnailFile ) }} in twig?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无声无音无过去 2025-01-12 05:13:28

根据 Kriswallsmith 的说法,使用 assetic 是不可能实现这一点的。

https://github.com/kriswallsmith/assetic/issues/60

如果我理解正确的话:
由于 assetic 不会渲染 Twig,因此不会读取变量。

According to Kriswallsmith this is not possible using assetic.

https://github.com/kriswallsmith/assetic/issues/60

If I understood correctly:
Since assetic does not render the Twig the variable is not being read.

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