You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Jinja is great for a lot of things, but one issue it has is breaking syntax within the file, and becoming its own type. If I have a Jinja / HTML template, I can't format it with prettier, for instance.
It's a pretty common thing to have a very basic template, where common elements are shared.
What if we could combine the template and output file into the same file? Obviously, this isn't for everyone, or every use case, but for something simple like a blog, probably totally fine.
We could start with this, which is perfectly valid HTML:
<html><body><!-- BEGIN JINJA: {{ links }} --><!-- END JINJA --><article>Stuff</article></body></html>
I imagine something like a jinjafy --check html/*.html, which exits 1, because changes can be made. But if I run jinjafy html/*.html (or call some Python directly), I get this.
<html><body><!-- BEGIN JINJA: {{ links }} --><ahref="/">Home</a><ahref="/about">About</a><!-- END JINJA --><article>Stuff</article></body></html>
And if I run jinjafy --check html/*.html again, it exits 0 because the files are in sync. If I were to change links, I could update all of my files with the new output.
Now I can run prettier, or anything else on the file that I like. The file is always valid HTML, and it's still formatted with Jinja. Jinja just puts the output in between the BEGIN/END statements.
This could probably also work with some other type of files, like YAML. For instance:
key: value# BEGIN JINJA: {{ details }}details: ["I", "have", "a", "lot", "of", "details"]# END JINJA
One could strip out the comments in final processing (as often happens with an HTML minifier), or leave them in. Either way, the files stay their valid type, with editors being able to correctly format files as they are.
I realize this may be beyond Jinja's scope, but I feel like such a tool would ideally utilize jinja as it offers lots of flexibility.
I've searched and haven't found anything like this, nor this type if "in-place" templating.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Jinja is great for a lot of things, but one issue it has is breaking syntax within the file, and becoming its own type. If I have a Jinja / HTML template, I can't format it with
prettier
, for instance.It's a pretty common thing to have a very basic template, where common elements are shared.
What if we could combine the template and output file into the same file? Obviously, this isn't for everyone, or every use case, but for something simple like a blog, probably totally fine.
We could start with this, which is perfectly valid HTML:
I imagine something like a
jinjafy --check html/*.html
, which exits 1, because changes can be made. But if I runjinjafy html/*.html
(or call some Python directly), I get this.And if I run
jinjafy --check html/*.html
again, it exits 0 because the files are in sync. If I were to changelinks
, I could update all of my files with the new output.Now I can run
prettier
, or anything else on the file that I like. The file is always valid HTML, and it's still formatted with Jinja. Jinja just puts the output in between the BEGIN/END statements.This could probably also work with some other type of files, like YAML. For instance:
One could strip out the comments in final processing (as often happens with an HTML minifier), or leave them in. Either way, the files stay their valid type, with editors being able to correctly format files as they are.
I realize this may be beyond Jinja's scope, but I feel like such a tool would ideally utilize jinja as it offers lots of flexibility.
I've searched and haven't found anything like this, nor this type if "in-place" templating.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions