-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: text/templates: custom block preprocessor #71196
Comments
Related Issues (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
cc @robpike allowing the creation of a new DSLs within text/template feels out of scope? as in it becomes a different language entirely. |
@seankhliao - +1 We would not want to create a new DSL and the current syntax/structure would be preserved. I was thinking of this in the same philosophy to the "block" action which is equivalent to a "define" followed by a "template". Is that fair? |
block and define/template allow for reuse and override of content, but it doesn't change the way you interact with the templating language. this proposal (a preprocessor) can fundamentally change what the language looks like, switch/case and import both represent shifts in control flow, enough for it to be considered a DSL (just because everything is wrapped in {{}} doesn't mean it's the same syntax). I think #54748 is loosely related, though it operates on already rendered template blocks. |
@seankhliao - I appreciate the concerns about maintaining Go's simple template design. You raise valid points about DSLs - we definitely want to avoid fragmenting the ecosystem or creating a "template within a template" situation. One thing I hadnt added here (due to this being a starting point) is to keep transformations predictable and inline with the Go templating philosophy. For example we could add validators to ensure that only standard nodes are produced (Though today this can still be hand-coded explicitly). Couple of other advantages I wanted to share:
|
Proposal Details
I wanted to share a feature request that I have been seeking for a long time. Would love to get feedback and see the appetite for this. The templating package is simple and yet powerful by design. Today the actions are hard coded in the parser (text/template/parse/parse.go). Templating languages like Jinja offer custom "nodes" - extensions for user defined action types instead of the fixed ones defined by the template parser. This would allow users to build custom functionality at parse time itself.
Example Usage:
Goals:
In this proposal when a custom action is encountered the child is parsed and passed to the action instead of the action driving its own parser - this is in line with (2). Doing so will incur an inefficiency. However this is optin and parsed templates can be cached making this a one off.
To address (7). This proposal is only for a parser. By making the parser only act as a preprocessor and rewriting the parse tree, the standard exec phase can apply. For example a "switch" action could rewrite the body in terms of a "if/else-if" actions.
Use cases
"switch" action
"import" action
Instead of loading all templates, we could only provide a root template that loads its dependencies providing better isolation:
The text was updated successfully, but these errors were encountered: