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
It would be very nice to have the possibility to parse a stringbuffer as caddy template when using templates.
Right now there already is include "<filepath>" args... and httpInclude "<uri>" args...
it would be natural to also be able to include(parse) a random string as template. This is very useful for instance to assign templated json to a variable in a template file.
Feature
bufferInclude "<content>" args...
Not sure if bufferInclude is descriptive enough...
Example Use-Case
I might have a strange use-case for this, but i think it would be very nice and only consistent with the current functions to also have the possibility to parse a random string as tamplate.
For my use case i needed to combine 3 json templates into 1 and parse it and assign it to a template var.
Right now i simply have a file called include.json that looks a bit like this:
{{- $requestPath := index .Args 0 -}}
{{- $requestSpecificJson := index .Args 1 -}}
{
"tag1": {{- include "/file1.json" -}},
"tag2": {{- include "/file2.json" -}},
"tag3": {{- include "/file3.json" -}},
"someSpecialTag": {{- include $requestSpecificJson $requestPath -}}
}
and i include it like that: {{- $myMetadata := include "/include.json" $requestPath $requestSpecificJson | mustFromJson -}}
With bufferInclude i could get rid of the file completely.
I already have a custom template function extension for caddy that generates a json string buffer like this. But I found no way to execute all the placeholders etc. within the templates(file1,file2,file3 are all using caddy placeholders)
Currently i found no way to let caddy parse it as a template to resolve the caddy specific placeholders, include other files and basically all the functionality caddy build on top of the golang standard template functions.
I think as soon as people start to write custom template functions this will be useful for a lot of use-cases.
Current Workaround
I simply write a template and save it as a file that combines the other templates and then parse the file as json to be able to assign it as a var.
Alternate approach
An alternate approach would be to, pass the template context to template extensions so an extension can call executeTemplateInBuffer and all the other caddy and sprig template functions directly. But i thing the first would be the solution is way more flexible, as for instance also third party template functions could be executed.
Proposed solution
A function that basically is 1:1 the same as include but instead of a filename it takes a string(content buffer) as an argument.
// funcIncludeBuffer takes a string buffer as input and renders it in place.
// Note that included buffer is NOT escaped, so you should only include
// trusted content. If it is not trusted, be sure to use escaping functions
// in your template.
func (c TemplateContext) funcIncludeBuffer (content string, args ...any) (string, error) {
//-- create and fill bodyBuf with the provided string
c.Args = args
err = c.executeTemplateInBuffer(filename, bodyBuf)
if err != nil {
return "", err
}
return bodyBuf.String(), nil
}
I hope i made sense so far, if there is a better solution or i missed something please let me know!
And thanks a lot for the fantastic caddyserver! I really enjoy tinkering with it....
The text was updated successfully, but these errors were encountered:
After some more digging and getting to know how caddy modules work. I guess it might be a better approach, for my use-case to run a custom middleware/plugin rather than relying on templates after all. 😀 as most of the custom logic should be in the middleware and not in the templates anyway.
I guess we could consider this one closed for now...
Still figuring stuff out, sorry for bothering you with an non-issue(i'll use the community forum first in the future)... and keep up the great work! ;-)
It would be very nice to have the possibility to parse a stringbuffer as caddy template when using templates.
Right now there already is
include "<filepath>" args...
andhttpInclude "<uri>" args..
.it would be natural to also be able to include(parse) a random string as template. This is very useful for instance to assign templated json to a variable in a template file.
Feature
bufferInclude "<content>" args...
Not sure if bufferInclude is descriptive enough...
Example Use-Case
I might have a strange use-case for this, but i think it would be very nice and only consistent with the current functions to also have the possibility to parse a random string as tamplate.
For my use case i needed to combine 3 json templates into 1 and parse it and assign it to a template var.
Right now i simply have a file called include.json that looks a bit like this:
and i include it like that:
{{- $myMetadata := include "/include.json" $requestPath $requestSpecificJson | mustFromJson -}}
With bufferInclude i could get rid of the file completely.
I already have a custom template function extension for caddy that generates a json string buffer like this. But I found no way to execute all the placeholders etc. within the templates(file1,file2,file3 are all using caddy placeholders)
Currently i found no way to let caddy parse it as a template to resolve the caddy specific placeholders, include other files and basically all the functionality caddy build on top of the golang standard template functions.
I think as soon as people start to write custom template functions this will be useful for a lot of use-cases.
Current Workaround
I simply write a template and save it as a file that combines the other templates and then parse the file as json to be able to assign it as a var.
Alternate approach
An alternate approach would be to, pass the template context to template extensions so an extension can call executeTemplateInBuffer and all the other caddy and sprig template functions directly. But i thing the first would be the solution is way more flexible, as for instance also third party template functions could be executed.
Proposed solution
A function that basically is 1:1 the same as include but instead of a filename it takes a string(content buffer) as an argument.
I hope i made sense so far, if there is a better solution or i missed something please let me know!
And thanks a lot for the fantastic caddyserver! I really enjoy tinkering with it....
The text was updated successfully, but these errors were encountered: