Change Templating Language to Nunjucks
Apr 3, 2024Changing the template language
I'm working on a page that shows posts by tag, allowing readers to filter based on what they want to see. As I've been working on this, I've come across some limitations of the liquid
templating language that is used by default for Markdown files. Well, perhaps not limitations of the lanugage, more like limitations of my knowledge of it and lack of ability to find good examples online that use it. The result is the same.
11ty makes it easy to change the template language per filetype though. All that I had to do was add this to my module.exports
, at the end:
return {
markdownTemplateEngine: "njk",
};
Fixing what that broke
This broke the feature on my index page that showed the latest post. It turned out to only be a minor syntax issue. Instead of using:
collections.post.last.something
I instead had to use a filter. For ease of use, I also set it to a variable:
set lastpost = collections.post | last
My new template looks like this. Again, remember that the curly braces should be duplicated in actual code:
<a href={lastopst.url}>{lastpost.data.title}</a> - {lastpost.date | postDate }