DEV Community

Cover image for Highlight Nunjucks code blocks with twig
Mathieu Huot
Mathieu Huot

Posted on

Highlight Nunjucks code blocks with twig

I just discovered lately that, when writing Markdown, I can highlight Nunjucks code blocks using the twig flag! At least it works with Dev.to Markdown flavor. I had previously tryed the nunjucks tag without success and the html was missing some key highlights. Here's a comparition of the three.

With the nunjucks tag

---
permalink: formations/{{ video.snippet.title | lower | slug }}/
---
<section class="listing-section">
  {% for video in videos %}
    {% card
    defaultClass = 'listing-card',
    title = video.data.title,
    detail = video.data.publishedat | timeFormat,
    text = video.data.description,
    link = video.url | url,
    linkText = 'participe à la formation!'
    %}
  {% endfor %}      
</section>
Enter fullscreen mode Exit fullscreen mode

With the html tag

---
permalink: formations/{{ video.snippet.title | lower | slug }}/
---
<section class="listing-section">
  {% for video in videos %}
    {% card
    defaultClass = 'listing-card',
    title = video.data.title,
    detail = video.data.publishedat | timeFormat,
    text = video.data.description,
    link = video.url | url,
    linkText = 'participe à la formation!'
    %}
  {% endfor %}      
</section>
Enter fullscreen mode Exit fullscreen mode

With the twig tag

---
permalink: formations/{{ video.snippet.title | lower | slug }}/
---
<section class="listing-section">
  {% for video in videos %}
    {% card
    defaultClass = 'listing-card',
    title = video.data.title,
    detail = video.data.publishedat | timeFormat,
    text = video.data.description,
    link = video.url | url,
    linkText = 'participe à la formation!'
    %}
  {% endfor %}      
</section>
Enter fullscreen mode Exit fullscreen mode

Ah, much better! Thanks for reading! 😊

Discussion (0)