Search code examples
symfonytwigassetic

Including an image in the symfony transation file with assetic?


I want to include an image in the symfony translation file with assetic, how can this be done?

<![CDATA[Some text<br><img scr=" generated url with assetic "><br>some text]]>

Solution

  • You should use translation parameters. Instead real src in translation you should have parameter name.

    <![CDATA[Some text<br><img scr="%variableName%"><br>some text]]>
    

    Example usage in twig template

    {{ "your.translation.with_parameter"|trans({'%variableName%': assetUrl}, "domain") }}
    

    You can get your asset uri path from controller with this code:

    $myAssetUrl = $this->container
        ->get('templating.helper.assets')
        ->getUrl('bundles/acmedemo/images/header.png');
    

    Read more about assets helper in Symfony documentation: http://symfony.com/doc/current/components/templating/helpers/assetshelper.html