【问题标题】:Symfony2 Path to image in twig templateSymfony2 树枝模板中图像的路径
【发布时间】:2013-04-30 03:47:47
【问题描述】:

我将 img 存储在Acme/DemoBundle/Resources/public/img/weather_icon/blizzard.gif 我想把这个img放在我的模板中,所以我做了

<img src="{{ asset('bundles/acmedemo/img/weather_icons/blizzard.gif') }}" />

<img src="{{ asset('..img/weather_icons/blizzard.gif') }}"  />

这没有用。我做了资产:安装和资产:转储

已解决 成功了

<img src="{{ asset('img/weather_icons/Blizzard.gif') }}" alt="Symfony!" />

【问题讨论】:

  • 第一个语法很好。 -- 一些想法:检查web/bundles/acmedemo/img/weather_icons/blizzard.gif 文件是否存在(很容易打错字)。
  • @konadrian:只是为了澄清您的答案是没有 Assetic 的用法,您只需直接提供存储在应用程序中的文件。与直接提供文件相比,使用 Assetic 提供了许多优势。这些文件不需要存储在提供它们的位置,并且可以从各种来源中提取,例如从包中提取。

标签: symfony twig assetic


【解决方案1】:

请试试这个

<img src="{{ asset('bundles/acmedemo/img/weather_icons/blizzard.gif') }}" />

您应该使用命令将资产安装到 web 目录中

app/console assets:install web

【讨论】:

    【解决方案2】:

    您可以使用捆绑包的图像:

    {% image '@CoreBundle/Resources/public/images/your-image.jpg'  output="images/your-image.jpg" %}
    <img src="{{ asset_url }}" width="100%" height="100%" alt="my alt of image" class="pull-left">
    {% endimage %}
    

    【讨论】:

      【解决方案3】:

      Assetic 解决方案:与 Assetic 相比,使用 Assetic 可以获得更好的性能。

      目录结构例如:

      C:\xampp\htdocs\yourproject\src\AppBundle\Resources\public\images\yourimage.png
      

      项目结构举例:

      yourproject\src\AppBundle\Resources\public\images\yourimage.png
      

      在您的html.html.twig 调用中:

      {% image '@AppBundle/Resources/public/images/yourimage.png' %}
                          <img src="{{ asset_url }}" alt="Example" />
                      {% endimage %}
      

      注意:

      这是为您提供安装资产包,可以在项目的 composer.json 中设置,如下所示:

      "require": {
      "php": ">=5.3.3",
      "sensio/framework-extra-bundle": "~3.0",
      "symfony/assetic-bundle": "~2.6", },
      

      google 以获取更多关于安装资产包 symfony2 的说明。

      就是这样。

      来源:

      http://symfony.com/doc/2.7/cookbook/assetic/asset_management.html#cookbook-assetic-cssrewrite

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-27
        • 1970-01-01
        • 1970-01-01
        • 2014-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多