树枝:包含来自SVG文件的外部代码
问题描述:
是否有可能将svg文件中的代码直接插入树枝模板文件中?树枝:包含来自SVG文件的外部代码
喜欢的东西:
{% include 'my.svg' %}
,这将导致:
<svg viewbox=".... />
答
您可以在Drupal8主题通过设置一个新的变量做:
function theme_preprocess_page(&$variables) {
$svg = file_get_contents(drupal_get_path('theme', 'socialbase') . '/images/icons.svg');
$variables['svg_sprite'] = t('svg', array('svg' => $svg));
}
在您的树枝文件你可以打印它:
{{ svg_sprite }}
答
有一个类似的问题,最终将我的svgs重命名为.twig文件。这样做的
{% include 'my.svg.twig' %}
+1
是的,这里一样;) –
答
方式一:
{{ source('my.svg') }}
在这里阅读更多: https://www.theodo.fr/blog/2017/01/integrating-and-interacting-with-svg-image-files-using-twig/
答
对于我来说,它的工作:
{% include '/images/my.svg' %}
只是快速的更新,如果你正在使用Drupal 8,因为在P的代码先前的回答对我无效。这是我做的:
function theme_preprocess_page(&$variables) {
$svg = file_get_contents(drupal_get_path('theme', 'theme_name') . '/images/my.svg');
$variables['some_svg'] = $svg));
}
而在树枝文件I outputed这样的:
{{ some_svg|raw }}
答
在主题的情况下,它使用{{ directory }}
变量保存到主题路径不错的选择。
{{ source(directory ~ '/images/my.svg') }}
如果你把那个文件放在你的模板文件夹中,那么肯定是 – DarkBee