loader

Find the relevant Jinja template location and load that into the environment.

Render a Jinja Template

First, we construct a Jinja environment by attempting to load Jinja templates from a variety of locations in a preferential order. These locations are listed below in order of precedence:

  1. A _quartodoc_templates/ folder located at the root of your quarto project (in the same directory as _quarto.yml).
  2. The ~/.quartodoc/templates folder (located in your home directory).
  3. The templates located in the griffe_quarto python package. You can see them on GitHub here (make sure you are referencing the correct version if you have pinned that).

This precedence is important when using with template inheritance. An example is shown below.

Consider the following example where we have two templates:

  1. the parent template, qmd.tpl located in the griffe_quarto python package (in the templates/ folder).
  2. the child template, test.tpl located in this quarto project in the _quartodoc_templates/ folder.
!cat ../griffe_quarto/templates/qmd.tpl
{% block frontmatter %}
---
title: {{ title }}
{% if description %}
description: {{ desc }}
{% endif %}
---
{% endblock frontmatter %}

{% block body %}{% endblock body %}
!cat _quartodoc_templates/test.tpl
{% extends "qmd.tpl" %}
{% block body %}
A test template with the variable `foo`: {{ foo }} 
Another line with the variable `bar`: {{ bar }}
{% endblock %}

We can render these templates like so:

Note that test.tpl inherits from qmd.tpl.

template = env().get_template("test.tpl")

_rendered = template.render(title='A Test Title', 
                            description=None, 
                            foo='abc123', 
                            bar='xyz987')
print(_rendered)
---
title: A Test Title
---

A test template with the variable `foo`: abc123 
Another line with the variable `bar`: xyz987