feat: replace Handlebars with Nunjucks in TEMPLATE renderer#198
Open
victoraraujo105 wants to merge 2 commits intoh-sphere:mainfrom
Open
feat: replace Handlebars with Nunjucks in TEMPLATE renderer#198victoraraujo105 wants to merge 2 commits intoh-sphere:mainfrom
victoraraujo105 wants to merge 2 commits intoh-sphere:mainfrom
Conversation
Swap template engine from Handlebars to Nunjucks for richer template capabilities. Nunjucks provides native groupby filter, macros, set/variables, and recursive calls without SQL workarounds. Changes: - TemplateRenderer.ts: use nunjucks.compile/render, add groupby/unique filters - nunjucksHighlighter.ts: syntax highlighting for Nunjucks tags/expressions - parser.ts: rename handlebarsTemplate -> nunjucksTemplate in grammar - package.json: swap handlebars dep for nunjucks
This was referenced Feb 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace the Handlebars template engine with Nunjucks in the TEMPLATE renderer to enable vault-based template reuse and provide a more expressive templating layer.
Motivation
The core problem: template reuse is impossible with Handlebars
Users who build custom table layouts in
TEMPLATEblocks must duplicate the entire template in every code block. When multiple notes query similar data (e.g. task lists with Azure DevOps links, GitLab MR links, internal vault links), the same 20-30 line template is copied verbatim each time. Changing the layout means updating every copy.The natural solution is to let users define templates in vault files and reference them via
{% include %}or{% from ... import %}. This is architecturally impossible with Handlebars. Handlebars partials must be registered programmatically viaHandlebars.registerPartial()before compilation — there is no loader interface that allows resolving templates from an external source at render time.Why Nunjucks
Nunjucks (a Jinja2 port for JavaScript) has a native
Loaderarchitecture: itsEnvironmentconstructor accepts aLoaderobject that resolves template names to source content. This is the exact extension point needed to load.njkfiles from the Obsidian vault. A follow-up PR (#199) implements this loader.Beyond the loader architecture, Nunjucks also provides practical improvements for template authors:
groupby/uniquefilters — group and deduplicate data in the template instead of pushing complexity into SQL (GROUP_CONCAT, subqueries){% set %}variables — intermediate computations without extra SQL columns{% macro %}definitions — reusable template functions within a single blockSyntax migration
{{#each data}}...{{/each}}{% for row in data %}...{% endfor %}{{#if condition}}...{{/if}}{% if condition %}...{% endif %}{{#unless x}}...{{/unless}}{% if not x %}...{% endif %}{{this.field}}{{ row.field }}(explicit loop var){{@index}}{{ loop.index0 }}Example
Before (Handlebars):
After (Nunjucks):
Changes
package.json: Replacehandlebarsdependency withnunjucksTemplateRenderer.ts: Rewrite to usenunjucks.compile/nunjucks.runtime.SafeString. Addgroupbyanduniquecustom filters.parser.ts: Rename grammar rulehandlebarsTemplate→nunjucksTemplatenunjucksHighlighter.ts(new): Syntax highlighting for Nunjucks tags ({% %}), expressions ({{ }}), comments ({# #}), filters (|), and keywordshandlebarsHighlighter.ts(deleted): Replaced bynunjucksHighlighter.tshighlighterOperation.ts: Import updated to usenunjucksHighlighterBreaking Change
Existing templates using Handlebars syntax (
{{#each}},{{#if}},{{#unless}}) must be updated to Nunjucks equivalents. The{{ variable }}output syntax remains identical — only block helpers and iteration syntax change.Testing
Follow-up
PR #199 adds a
VaultLoaderthat enables{% include %}and{% from ... import %}to load.njktemplate files from the vault — the primary use case that motivated this engine swap.