【问题标题】:How to mix Pug/Jade with Django conditionals and html element attributes?如何将 Pug/Jade 与 Django 条件和 html 元素属性混合?
【发布时间】:2019-10-01 13:39:14
【问题描述】:

我想为 html 属性编写 Django 条件,比如

<a {% if item.link %} href="{{ item.link }}", target="_blank", rel="noopener", aria-label="{{ item }}" {% endif %}>
     --- Content ---
</a>

我正在使用 pug/jade,所以我不能将 jam/pug 语法放在“内容”块中,编译器会中断。 我想知道我是否可以以任何方式处理此问题以不重复“内容”块。

我也试过了,没有成功:

a({% if item.link %} href="{{ item.link }}", target="_blank", rel="noopener", aria-label="{{ item }}" {% endif %})
      ----Content---

【问题讨论】:

  • 你试过使用 PyPugJS 之类的东西吗? github.com/kakulukia/pypugjs
  • 环顾四周,但没有看到该软件包在哪里解决了我的问题...
  • 这是一个用于编写 pug 的系统,可编译为 django 语法。你编写的 pug 条件句会被翻译成 django 条件句。我认为您不会找到同时使用它们的好方法。

标签: django django-templates pug templatetags


【解决方案1】:

我终于找到了实现这一目标的方法。令人难以置信的解决方案非常简单,当我找到它时我感到有点愚蠢:

<a {% if item.link %} href="{{ item.link }}", target="_blank", rel="noopener", aria-label="{{ item }}" {% endif %}>
--- Content ---
</a>

只需要将 --- Content --- 块放在与纯 html 内容相同的缩进级别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多