在树枝模板中使用 theme/image 文件夹中的图像

Use image from theme/image folder in twig template

我想包含来自 custom_theme/images 文件夹的图像,其中 custom_theme 是根据 grav 文档创建的自定义主题。

twig 模板是部分片段,这是摘录:

<!-- Logo -->
<a class="navbar-brand" href="./">
    <img class="logo logo-dark" alt="" src="/images/logo.png">
    <img class="logo logo-light" alt="" src="/images/logo-light.png">
</a>
<!-- #end Logo -->

src 中应使用什么来引用此图像文件:/user/themes/custom_theme/images/logo.png

你看过Grav documentation了吗?我在搜索字段中输入 "image" 并找到了这个:

Twig Filters & Functions

Url

Will create a URL and convert any PHP URL streams into a valid HTML resources. A default value can be passed in in case the URL cannot be resolved.

url('theme://images/logo.png')|default('http://www.placehold.it/150x100/f4f4f4')/user/themes/learn2/images/logo.png

所以你应该有:

<!-- Logo -->
<a class="navbar-brand" href="./">
    <img class="logo logo-dark" alt="" src="{{ url('theme://images/logo.png') }}">
    <img class="logo logo-light" alt="" src="{{ url('theme://images/logo-light.png') }}">
</a>
<!-- #end Logo -->

如果你愿意,你可以使用基本的default Twig filter to provide a fallback URL if the image can't be found. If you don't use the default filter and the image can't be found, the url function will return nothing (or actually null, but it will be converted into an empty string), as can be seen in the source code of the url function