覆盖捆绑中的图像

时间:2013-05-21 14:10:17

标签: symfony

我有这个:

ShopBundle
  Controller
  Resources
    public
      images
        marker.png
    views
      Default
        index.html.twig

在我的index.html.twig中,我想要这个

<img src="{{ asset("images/marker.png") }}"/>

我喜欢使用我的捆绑包的人,他们只想使用他们自己的marker.png来构建一个继承我的捆绑包,并通过以下文件结构放置他们的图像:

MyShopBundle
  Resources
    public
      images
        marker.png

在Symfony2中有没有简单的方法可以做到这一点?这种需求似乎很简单,我无法相信我没有找到答案。

所以,

  • 如何在捆绑资源目录中的捆绑模板中包含图像资源?我已经做了./apps/hfr/console assets:install web,但我的模板没有打印正确的网址(/ images / marker.png而不是/ bundles / ShopBundle / Resources / public / images / png)

  • 是否可以覆盖我想要的方式或者我迷失了方向?

1 个答案:

答案 0 :(得分:6)

解决方案:

使用@ syntax ...

{% image '@VendorMyShopBundleBundle/Resources/public/images/example.jpg'
    output='/images/example.jpg' %}
    <img src="{{ asset_url }}" alt="Example"/>
{% endimage %}

请注意,您的网络服务器通常无法访问 Vendor / YourBundle / Resources / public

资产:install命令因此将资产复制到 web / bundles / vendoryourbundle

如果您使用开发环境,{{asset('path / to / asset.jpg')}}函数将调整您的资源的网址:

 http://hostname/app_dev.php 

/path/to/asset.jpg 

/app_dev.php/to/asset.jpg

[编辑]

如果您想要更多地控制资产,可以考虑使用资产集

您可以按如下方式配置它们:

# app/Resources/config/config.yml

assetic:
    [...]
    assets:
        css_bootstrap:
            inputs:
                -  %kernel.root_dir%/../src/Vendor/YourBundle/Resources/public/twitter-bootstrap/less/bootstrap.less
                - [...]
            filters:
                - lessphp
                - [...]
            output: css/bootstrap.css

         my_image:
            inputs: 
                - %kernel.root_dir%/../path/to/image.png
            filters:
                - optipng
            output: images/image-with-new-name.png

然后在模板中使用它们,如下所示:

{% stylesheets '@css_bootstrap' %}
    <link rel="stylesheet" type="text/css" href="{{ asset_url }}">
{% endstylesheets %}

如果assetic / assets / packagename / inputs配置数组支持@VendorYourBundle语法,我现在还不确定,然后使用bundle继承。

<强>增加:

在使用这些软件包之前,您必须使用控制台命令:

php app/console assetic:dump