feat: add VaultLoader for reusable Nunjucks templates in TEMPLATE renderer#199
Open
victoraraujo105 wants to merge 4 commits intoh-sphere:mainfrom
Open
feat: add VaultLoader for reusable Nunjucks templates in TEMPLATE renderer#199victoraraujo105 wants to merge 4 commits intoh-sphere:mainfrom
victoraraujo105 wants to merge 4 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
Implement a custom Nunjucks loader that reads .njk template files from
the Obsidian vault, enabling {% include %} and {% from ... import %}
directives in TEMPLATE renderer code blocks.
- VaultLoader preloads all .njk files into a cache at startup
- File watchers keep the cache in sync on create/modify/delete/rename
- Event registration uses plugin.registerEvent for proper cleanup
- TemplateRenderer now accepts an optional VaultLoader instance
- Moved nunjucks Environment into TemplateRenderer class (was module-level)
- Added Jest obsidian mock and 15 unit tests covering loader, nunjucks
integration (include, import, nested includes), and file watchers
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
Add a custom Nunjucks loader (
VaultLoader) that enables{% include %}and{% from ... import %}directives in the TEMPLATE renderer, allowing users to define reusable templates in.njkfiles within their vault.Motivation
Even with the Nunjucks migration (#198), each TEMPLATE code block must contain its full template inline. Users who have multiple queries with similar table layouts must duplicate the entire template in every code block.
With this change, users can create
.njkfiles in their vault and reference them:Or use Nunjucks macros for composable, DRY templates:
Template paths are relative to the vault root, matching the convention used by
TABLE data = file(path.csv).Changes
VaultLoader.ts(new): Custom Nunjucks loader that preloads all.njkfiles from the vault into a cache at startup. File watchers (create/modify/delete/rename) keep the cache in sync. Usesplugin.registerEvent()for proper cleanup on plugin unload.TemplateRenderer.ts: Movednunjucks.Environmentfrom module-level into the class constructor so it can accept theVaultLoader. Custom filters (groupby,unique) extracted into aregisterFiltershelper. The loader parameter is optional for backwards compatibility.init.ts: Creates theVaultLoader, preloads templates inonLayoutReady(before codeblock handlers are registered), and passes it toTemplateRenderer.Testing
getSource— exact path, extension inference, missing template errorloadAll— filters to.njkonly, handles multiple files{% include %},{% from ... import %}, extension-less includes, nested includesNotes
_templates(notcache) to avoid conflict with Nunjucks'Environment.initCache()which overwrites each loader'scacheproperty with a plain object.src/__mocks__/obsidian.tsand updatedjest.config.jswithmoduleNameMapperandesModuleInteropto support testing modules that import fromobsidian.Closes #197