Question

This is my architecture folders :

This is my architecture folders

I would like to load an picture using asset() in a twig template which is located in

sites/sf2.local/views/index.html.twig

and the picture is in

sites/sf2.local/assets/img/logo_om.gif

So in my config.yml I have added this line :

assetic:
    read_from:      %kernel.root_dir%/../sites/sf2.local/assets/

but it doesn't work, I can't load the picture using :

<img src="{{ asset('img/logo_om.gif') }}" alt="img" />

in the twig template..

How can I do this ?

Was it helpful?

Solution

First, I think you have mistaken assets for assetic. Twig function asset does nothing more than making you assets absolute. It does not affect where those assets are read from. That is described in this docs:

Linking to assets

On the other hand, assetic describes where compiled (and optionally filtered, merged, uglified, etc...) resources are read from. That only applies to JS and CSS.

Possible solution:

Open Terminal, navigate to your web symlink that directory:

cd /.../sf2/web
ln -s ../sites/sf2.local/assets/img
ln -s ../sites/sf2.local/assets/js
ln -s ../sites/sf2.local/assets/css

Now you have 3 additional symlinks (not copies) in your web and using asset('img/some-pic.png') actually targets the symlinked img directory.

Is this symlinking acceptable?

Alternative solution (whole assets):

cd /.../sf2/web
ln -s ../sites/sf2.local/assets

and use it:

asset('assets/img/some-pic.png')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top