Twig: Include external Code from an SVG file

前端 未结 6 1143
臣服心动
臣服心动 2021-01-02 03:05

Is it possible to inlcude code from a svg file directly into a twig template file?

Something like:

{% include \'my.svg\' %}

that wi

6条回答
  •  清酒与你
    2021-01-02 03:47

    For me, it worked:

    {% include '/images/my.svg' %}
    

    Just a quick update if you are using Drupal 8 because the code in the previous answer didn't work for me. This is how I did it:

    function theme_preprocess_page(&$variables) {
      $svg = file_get_contents(drupal_get_path('theme', 'theme_name') . '/images/my.svg');
      $variables['some_svg'] = $svg;
    }
    

    And in the twig file, output using raw filter, or else it will escape the SVG markup:

    {{ some_svg|raw }}
    

提交回复
热议问题