-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add token preprocessor hook and URL query parameters #33
Conversation
If I understand correctly, you want to run preprocessors to modify the tags before the compilation in order to transform, filter or generate more tags. And what you are proposing is the ability to load a template multiple times with different options // Load with a fragment
const frag = await env.load("my-template.vto", undefined, { fragment: "button" });
// Load with another fragment
const frag = await env.load("my-template.vto", undefined, { fragment: "other" }); I don't think it's a good idea, it makes the cache invalidation more complicated, and it's confusing if you're importing the same file multiple times in your templates with different configurations. The preprocessor idea could be useful for some use cases, but I don't like the |
Right, though this particular use case it's a lot more convenient to do it this way. Otherwise, you'd need to have some additional runtime conditions or split it off into a new file.
Agreed, not really a fan of context either. It definitely complicates caching, so instead I think supporting query parameters in the path like so: const frag = await env.load("my-template.vto#fragment"); Would be a better approach, and can it be cached easily since it's just part of the path. Older versions of Nunjucks supported something like this (but that got lost in translation a few years ago unfortunately). However, the file loader would need to support it and trim off the query params. If you like, we could split that off into another PR, since preprocessing by itself is already pretty useful. |
Okay. I think this can be split in two different steps:
what do you think? |
Yep, perfect. I'll try that. |
95d8d40
to
6a76854
Compare
I also changed the name of the hook to "tokenPreprocessors" to make it a bit more specific, and to leave room for "preprocessors" in the future (ie. minify HTML directly from source, which isn't easy to do in a token preprocessor I'd imagine). |
It looks great. Thank you! |
This adds a new lower-level hook called
tokenPreprocessors
toEnvironment
that lets plugins transform a template's tokens into other tokens. It's liketags
, except it isn't limited to a tag, and you output tokens.Also adds support for URL query parameters. The sanitized path is passed to the loader, and the full path (with query strings) is passed to the token preprocessor, which unlocks a few capabilities.
Motivation
One use for this is to add template fragments for the
htmx
library (https://htmx.org/essays/template-fragments/). Basically, you can mark up portions of the template that can be rendered individually. For example:Template Fragments Example Plugin
Lets you write
and if I add this
"#button"
query parameter,my template now renders just the button:
Design questions
Thanks.
TODO