Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mix Pug/Jade with Django conditionals and html element attributes?

I want to write Django conditionals for html attributes, like

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

I am using pug/jade, so I can't put jade/pug syntax inside "Content" block, the compiler breaks. I would like to know if I can handle that in any way to no repeat the "Content" block.

I tried also, withouth success:

a({% if item.link %} href="{{ item.link }}", target="_blank", rel="noopener", aria-label="{{ item }}" {% endif %})
      ----Content---
like image 477
Lu Chinke Avatar asked Nov 06 '22 17:11

Lu Chinke


1 Answers

I finally found a way to achieve this. Incredibly the solution is very simple and I felt a little dumb when I found it:

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

Just need to put the --- Content --- block in the same indentation level as the plain html content.

like image 162
Lu Chinke Avatar answered Nov 14 '22 14:11

Lu Chinke