覆盖包中的图像

2023-12-04

我有这个:

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 有没有简单的方法可以做到这一点?这个需求似乎如此简单,我不敢相信我还没有找到答案。

So,

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

  • 是否有可能超越我想要的方式,或者我迷失了方向?


解决方案:

使用@语法...

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

请注意供应商/YourBundle/资源/公共您的网络服务器通常无法访问。

因此,assets:install 命令会将资产复制到网络/捆绑包/vendoryourbundle

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

 http://hostname/app_dev.php 

from

/path/to/asset.jpg 

to

/app_dev.php/to/asset.jpg

[EDIT]

如果您想对资产有更多控制权,可以考虑使用资产收藏.

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

# 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 语法,然后使用包继承。

添加:

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

php app/console assetic:dump
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

覆盖包中的图像 的相关文章

随机推荐