Override image from package

I have it:

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

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

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

And I would like people to use my package, which just wants to use their own marker.png, just to create a set that inherits mine and put their image just following the file structure:

 MyShopBundle Resources public images marker.png 

Is there an easy way to do this in symfony2? The need seems so simple that I cannot believe that I could not find the answers.

So,

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

  • Is it possible to redefine what I want, or have I lost my way?

+4
source share
1 answer

Decision:

use the syntax @ ...

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

Please note that the Provider / YourBundle / Resources / public will NOT normally be available to your web server.

Assets: the installation command will copy the assets to web / bundles / vendoryourbundle

The {{asset ('path / to / asset.jpg')}} function will adjust the URL of your resource if you use 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 your assets, 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 in the template as follows:

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

I'm NOT sure right now if the assetic / assets / packagename / input configuration array supports @VendorYourBundle syntax and then uses package inheritance.

Addition:

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

 php app/console assetic:dump 
+6
source

Source: https://habr.com/ru/post/1481967/


All Articles