Customizing Templates

Jan 10, 2025 Feature

vāk uses Nunjucks for templating. You have full control over your blog's HTML.

Template files

When you run vāk init, these templates are created in templates/:

File Purpose
index.html Homepage
post.html Individual post pages
archive.html Archive page with posts grouped by year
collection.html Collection pages
404.html Error page
feed.xml RSS feed
style.css Stylesheet
post.md Template for new posts

Available data

Each template receives specific data when rendered.

index.html

post.html

archive.html

collection.html

404.html

Nunjucks basics

{# Display a variable #}
{{ title }}

{# Render HTML content (for markdown) #}
{{ content | safe }}

{# Loop through posts #}
{% for post in allPosts %}
  <a href="./{{ post.slug }}">{{ post.title }}</a>
{% endfor %}

{# Conditionals #}
{% if collections %}
  Tagged in {{ collections | length }} collections
{% endif %}

{# Limit to first 5 posts #}
{% for post in allPosts %}
  {% if loop.index <= 5 %}
    {{ post.title }}
  {% endif %}
{% endfor %}

Common customizations

Add navigation

<nav>
  <a href="/">Home</a>
  <a href="/archive.html">Archive</a>
  <a href="/feed.xml">RSS</a>
</nav>

Show collections on posts

{% if collections %}
  {% for col in collections %}
    <a href="/collection-{{ col.slug }}.html">{{ col.name }}</a>
  {% endfor %}
{% endif %}

Group posts by year on homepage

{% for group in postsByYear %}
  <h2>{{ group.year }}</h2>
  {% for post in group.posts %}
    <li><a href="./{{ post.slug }}">{{ post.title }}</a></li>
  {% endfor %}
{% endfor %}

For more, see the Nunjucks documentation.