Override an image from a bundle

前端 未结 1 1641
余生分开走
余生分开走 2021-01-13 01:59

I have this:

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

相关标签:
1条回答
  • 2021-01-13 02:34

    Solution:

    use the @ syntax ...

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

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

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

    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 
    

    from

    /path/to/asset.jpg 
    

    to

    /app_dev.php/to/asset.jpg
    

    [EDIT]

    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 %}
    

    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.

    Addition:

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

    php app/console assetic:dump
    
    0 讨论(0)
提交回复
热议问题