【问题标题】:An exception has been thrown during the rendering of a template ("Warning: Illegal offset type in isset or empty")渲染模板期间抛出异常(“警告:isset 中的偏移类型非法或为空”)
【发布时间】:2018-12-23 02:53:33
【问题描述】:

我想在 twig 中显示我的用户上传的图像。

树枝:

<img src="{{ asset(user.photo|raw) }}" />

抛出此异常:

An exception has been thrown during the rendering of a template ("Warning: Illegal offset type in isset or empty").

我认为这是因为 user.photo 是一个文件字段。我试过这样做:

<img src="{{ asset(dossierPhoto ~ user.photo|raw) }}" />

在此示例中,我没有出现异常,但由于路径错误,因此未显示图像。 dossierPhoto 是图片所在的路径。

我正在使用 Webpack Encore 进行资产管理,但上传的文件不受它管理。

【问题讨论】:

  • 嗯,根据我的经验 - 您不需要资产功能来显示您的图像。您只需要图像路径作为 src。
  • 还有你使用过滤器raw的prop photo是什么?

标签: symfony twig


【解决方案1】:

试着写

<img src="{{ asset(user.photo|trans) }}" />

您的变量可能不是字符串,需要转换为一个,如related issue中所述

您可以使用{{ dump(user.photo) }} 来检查输出,如here 所述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多