February 2026

Fall Back to a Default Include File in MiniJinja

I'm working on upgrading the documentation for bitty . Everything is built with MiniJinja (rust's version of the Jinja template engine ).

Very section has live test code with it. Most of the tests trigger the same way. A few don't. I wanted a way that I could use a script to generate the default approach and then override it when I need. I'm doing with in three steps:

  1. Use a script to generate default files with names the begin with an underscore.
  2. Manually make files with the same name minus the underscore when I need to customize something.
  3. Have MiniJinja use the non-underscore version of the file if it exists. Otherwise, fall back to using the default one that starts with the character.

The file structure looks like this:

alfa-test/
  code_to_test.js
  _trigger.js
echo-test/
  code_to_test.js
  _trigger.js
  trigger.js

The goal is to use the trigger files:

alfa-test/_trigger.js
and
echo-test/trigger.js

I'm using this MiniJinja macro to make it happen:

[! macro get_content(folder, key) !]
    [! set default_content !]
        [! include [folder, "/_", key]|join("") !]
    [! endset !]
    [! set custom_content !]
        [! include [folder, "/", key]|join("") ignore missing !]
    [! endset !]
    [! if custom_content == "" !]
        [@ default_content @]
    [! else !]
        [@ custom_content @]
    [! endif !]
[! endmacro !]

It gets called like:

[@ get_content("alfa-test", "trigger.js") @]

[@ get_content("echo-test", "trigger.js") @]

In each case the base filename without the underscore is called. That's what gets used if it exists. Otherwise, the underscore fallback gets output.

This way I can just let the defaults do their thing then customize when necessary. Way better than having to duplicate everything myself.

-a

end of line

Endnotes

In some cases it would be possible to use a single copy of the default file stored in a central location. That won't work for what I'm doing. Each of the default trigger files has a unique ID in it that's generated outside of MiniJinja.

The default Jinja/MiniJinja tokens are hard for me to scan quickly. I switch them out for the square bracket versions you see here (e.g. {{ }} becomes [@ @].