phpsymfonysvgtwigsymfony-2.8

Including SVG in Twig template


I am changing my assets from PNGs to SVGs, where appropriate, however I am having difficultly including these SVGs in my Twig templates.

I am trying to include the SVG like this:

{{ source('/assets/img/crmpicco-horizontal-logo.svg') }}

However, this results in the following error:

Unable to find template "/assets/img/crmpicco-horizontal-logo.svg" (looked into: /Library/WebServer/Documents/crmpicco/symfony/app/Resources/views, /Library/WebServer/Documents/crmpicco/symfony/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form).

Why can't I include this SVG in the same directory as my other assets? Specifically, i'm interested in finding out why a SVG can't be treated as an asset with Assetic.


Solution

  • You have to rename your svg file with a twig extension crmpicco-horizontal-logo.svg.twig and do this :

    {% include ":svg:crmpicco-horizontal-logo.svg.twig" %}
    

    The folder svg is in app/Ressources/views in this example