Dynamic nesting templates

728 views
Skip to first unread message

Ken MacKenzie

unread,
Aug 10, 2015, 1:09:53 AM8/10/15
to golang-nuts
Been searching around on templating.  In particular my usage is html/template.

Oh and let me get this out of the way, experienced programmer, but new to go.  Building my first project with.  Personal use and experiment purpose.

OK so right now the way my app runs is something like this for building the templates.

I have a main.html and a posts.html.

I execute the main template with a structure that contains an array that is the posts.

Inside the main.html I do this:

{{template "post" .PagePosts}}

and inside the posts.html I iterate through it with a {{range....}}

Ok so yes all this is working but my thought is if there are no posts I still implement the post template.  Yeah not a big deal but I am curious about something more like this.

if I have posts, execute that array against the post template and save the resulting output to a variable.  Add that variable to the page struct content var and then process that against the main template.  Then instead of the {{template}} call it is just {{content}}.

The content will contain html so I will need to make it type template.HTML.

However part of me thinks this is kind of breaking the way templates should work

So an alternative....

Can I cast a template tag into a loaded template, basically if I know I have posts I replace the {{content}} tag with say {{template...}}.  Basically hot swapping in the template.

If all this sounds idiotic please excuse me.  The purpose of my program experiment is just sort of a blog engine at this point.  It is at this point a learning project.

Ken

Egon

unread,
Aug 10, 2015, 3:37:11 AM8/10/15
to golang-nuts
You lost me in the middle of the text. Usually posting your code makes things much clearer.


As for injecting content in -- the usual warnings of injection attacks apply -- but, you can cast it

content := template.HTML("<br>")

And then use that as a parameter to the template.

Alternatively you can also use a funcmap 
to return template.HTML and use that.

+ Egon

Ken MacKenzie

unread,
Aug 10, 2015, 11:04:44 AM8/10/15
to golang-nuts
Well my code shows the typical sub template usage.  Not the ideas I put forward.

Basically I am asking if I wanted to do dynamic templates can I do or more importantly should I do either of the following.

1.  Dynamically alter the cached template after parsing it in the program to insert a sub template call as needed.
2.  Have the template just allow a large block of HTML and use the sub template to build that html before passing it to the parent template.

If that doesn't make it clearer I can work up the two proposed styles and share via github.

Ken

Val

unread,
Aug 10, 2015, 11:36:54 AM8/10/15
to golang-nuts
Hi Ken
As text/template and html/template works basically the same way (except for some contextual formatting and security injection guards), you can write a whole working example with text/template in the Playground and share it with us.

For a blog engine, I guess you won't really need template injection. For conditionals, consider a {{if}} action . For more complex output, consider FuncMap as Egon suggested.

You made the right decision to try the standard packages, before third-party templating tools.
Cheers
Valentin

Ken MacKenzie

unread,
Aug 10, 2015, 11:43:20 AM8/10/15
to golang-nuts
I will work up an example in the next night or two.  My free development time is slightly on hold for a couple days because of work and life.

Yeah I like dealing more with a languages native features before getting too deep in third parties.  So far I think the only 3rd party I jumped right to is gorilla/mux for routing.  I am also trying to get my head around middle ware in go.  My starting point has been justinas/alice but I haven't really worked up a working implementation of that in my code.

Ken

Egon

unread,
Aug 10, 2015, 11:58:46 AM8/10/15
to golang-nuts


On Monday, 10 August 2015 18:04:44 UTC+3, Ken MacKenzie wrote:
Well my code shows the typical sub template usage.  Not the ideas I put forward.

Basically I am asking if I wanted to do dynamic templates can I do or more importantly should I do either of the following.

1.  Dynamically alter the cached template after parsing it in the program to insert a sub template call as needed.
2.  Have the template just allow a large block of HTML and use the sub template to build that html before passing it to the parent template.

If that doesn't make it clearer I can work up the two proposed styles and share via github.

The thing that is unclear to me is the actual context how you intend to use it. E.g. what are the security and performance implications, how are the dynamic templates built, who will write the templates, how do the templates get to the blog engine, how are they modified etc.

The FuncMap approach https://play.golang.org/p/KsjcjFNkmC ... although... remember that all the templates and "template.HTML" must be trusted, otherwise you'll get a possibility for injection attack.

+ Egon

Ken MacKenzie

unread,
Aug 10, 2015, 12:09:29 PM8/10/15
to golang-nuts
Actually you are hitting on I think my overall problem with my solutions so far and the usage of templates.  OK granted this is a side issue to my original post.  Anyway all my structs that define content type end up having a variable in them of type template.HTML.  Honestly what I would need if I went forward with this solution is something to sanitize that html and only leave some formatting tags and functionality for the <br> tag.  The original reason I even went with that was to get a newline through to html/template.

I should mention that as I am trying to approach this experiment from multiple angles I also have some duplicate routes defined to return JSON and process the front end through Angular instead.  That may be a better way to handle dynamic templating on that side.  Of course I am also a bit new to angular.  I had a skeleton angular front end I tested based on a static rendition of the expected output and that worked but it is not working right now against the golang feedback.  I know the problem an it is how my golang is structuring the json return.  On my todo list of next items to fix.  I know what is wrong with my golang json output however the odd thing is it is based off an restful api example I found from a site http://thenewstack.io/make-a-restful-json-api-go/.

My output in json is missing a couple things when I encode it against an array of my struct items.  Like the container parent description and commas between the items.

So I keep mentioning this in reference to posts but part of the reason I want to be able to do what I am saying is almost more CMS like and be able to dynamically place blocks of data in the layout of the site.

Ken

Egon Elbre

unread,
Aug 10, 2015, 4:08:03 PM8/10/15
to Ken MacKenzie, golang-nuts


On Monday, August 10, 2015, Ken MacKenzie <devil...@gmail.com> wrote:
Actually you are hitting on I think my overall problem with my solutions so far and the usage of templates.  OK granted this is a side issue to my original post.  Anyway all my structs that define content type end up having a variable in them of type template.HTML.  Honestly what I would need if I went forward with this solution is something to sanitize that html and only leave some formatting tags and functionality for the <br> tag.  The original reason I even went with that was to get a newline through to html/template.

You would need a separate user content sanitizer, quick search came up with this

There are probably more.

Essentially you can mix those with html/template and FuncMaps. If you want stricter escaping then html/template already contains funcs for it.
 

I should mention that as I am trying to approach this experiment from multiple angles I also have some duplicate routes defined to return JSON and process the front end through Angular instead.  That may be a better way to handle dynamic templating on that side.  Of course I am also a bit new to angular.  I had a skeleton angular front end I tested based on a static rendition of the expected output and that worked but it is not working right now against the golang feedback. 

Also, test injection attacks against both implementations to verify that you haven't made a mistake.
 
I know the problem an it is how my golang is structuring the json return.  On my todo list of next items to fix.  I know what is wrong with my golang json output however the odd thing is it is based off an restful api example I found from a site http://thenewstack.io/make-a-restful-json-api-go/.

My output in json is missing a couple things when I encode it against an array of my struct items.  Like the container parent description and commas between the items.

So I keep mentioning this in reference to posts but part of the reason I want to be able to do what I am saying is almost more CMS like and be able to dynamically place blocks of data in the layout of the site.

Ken

 
The thing that is unclear to me is the actual context how you intend to use it. E.g. what are the security and performance implications, how are the dynamic templates built, who will write the templates, how do the templates get to the blog engine, how are they modified etc.

The FuncMap approach https://play.golang.org/p/KsjcjFNkmC ... although... remember that all the templates and "template.HTML" must be trusted, otherwise you'll get a possibility for injection attack.

+ Egon

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/CV4_xTCGY8o/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages