覆盖包中的图像 [英] Override an image from a bundle

查看:28
本文介绍了覆盖包中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

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

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

In my index.html.twig, I'd like to have this

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

而且我希望使用我的包的人只想使用他们自己的marker.png 只需要构建一个继承我的包并通过以下文件结构放置他们的图像:

And I'd love people using my bundle who just want to use their own marker.png to just have to build a bundle inheriting mine and place their image just by following files structure:

MyShopBundle
  Resources
    public
      images
        marker.png

在 Symfony2 中有什么简单的方法可以做到这一点吗?需求似乎如此简单,以至于我无法相信我还没有找到答案.

Is there any simple way to do this in Symfony2 ? The need seems so simple that I can't believe I didn't find answers already.

所以,

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

  • How do you include an image asset in your bundle template from your bundle resources directory ? I already did a ./apps/hfr/console assets:install web but my template does not print the right url (/images/marker.png instead of /bundles/ShopBundle/Resources/public/images/png)

是否可以覆盖我想要的方式还是我迷路了?

Is it possible to override the way I want or did I lost my way ?

推荐答案

解决方案:

使用@语法...

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

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

Please note that Vendor/YourBundle/Resources/public will NOT be accessible by your web server normally.

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

The assets:install command will therefore copy the assets to web/bundles/vendoryourbundle

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

The {{ asset('path/to/asset.jpg') }} function will adjust the url of your asset if youre using the dev environment:

 http://hostname/app_dev.php 

来自

/path/to/asset.jpg 

/app_dev.php/to/asset.jpg

如果您想更好地控制资产,可以考虑使用资产集合.

if you want more control over the assets maybe consider using asset collections.

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

You can configure them as follows:

# 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

然后像这样在模板中使用它们:

and use them afterwards in your template like this:

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

我现在不确定 assetic/assets/packagename/inputs 配置数组是否也支持 @VendorYourBundle 语法,然后使用包继承.

I am NOT sure right now if the assetic/assets/packagename/inputs configuration array supports the @VendorYourBundle syntax aswell and then uses bundle inheritance though.

添加:

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

Before you can use these packages you will have to use the console command:

php app/console assetic:dump

这篇关于覆盖包中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆