Lu *_*nke 5 django django-templates templatetags pug
我想为html属性编写Django条件,例如
<a {% if item.link %} href="{{ item.link }}", target="_blank", rel="noopener", aria-label="{{ item }}" {% endif %}>
--- Content ---
</a>
Run Code Online (Sandbox Code Playgroud)
我正在使用pug / jade,因此无法将jade / pug语法放入“ Content”块中,编译器会中断。我想知道是否可以以任何方式处理该问题,而不重复“ Content”块。
我也尝试过,没有成功:
a({% if item.link %} href="{{ item.link }}", target="_blank", rel="noopener", aria-label="{{ item }}" {% endif %})
----Content---
Run Code Online (Sandbox Code Playgroud)
我终于找到了实现这一目标的方法。令人难以置信的是,解决方案非常简单,当我找到它时我感觉有点愚蠢:
<a {% if item.link %} href="{{ item.link }}", target="_blank", rel="noopener", aria-label="{{ item }}" {% endif %}>
--- Content ---
</a>
Run Code Online (Sandbox Code Playgroud)
只需将--- Content ---块放在与纯 html 内容相同的缩进级别中即可。
| 归档时间: |
|
| 查看次数: |
69 次 |
| 最近记录: |