【发布时间】:2021-02-28 21:07:00
【问题描述】:
我正在尝试使用 twig 有条件地在 HTML 图像标记中呈现标题属性(在 10 月 CMS 中)。
我尝试使用attributes.setAttribute 进行跟踪,但失败了:
<img class="my-class" src="{{ myImage.path }}" {{ myImage.title ? attributes.setAttribute('title', myImage.title) : {} }}>
有没有更好的方法来做到这一点?还是我真的必须这样做:
{% if myImage.title %}
<img class="my-class" src="{{ myImage.path }}" title="{{ myImage.title }}">
{% else %}
<img class="my-class" src="{{ myImage.path }}">
{% endif %}
我在 octoberCMS 中使用 twig...
【问题讨论】:
-
attributes.setAttribute 属性未定义。试试 this.setAttribute
-
你真的需要没有 title 属性吗?如果 myImage.title 是一个空字符串,您可以完全放弃在 html 中使用条件。它将呈现一个空的标题,在功能上与根本没有标题相同。
标签: html twig octobercms