Template redefine

207 views
Skip to first unread message

Metal3d

unread,
Jul 15, 2013, 7:05:26 AM7/15/13
to golan...@googlegroups.com
Hi,

This is not a problem, or a bug... This is a question to understand the rules of templates


As you can see, I try to set a "base" template (T1) and  children templates (T2). This code works as expected. I'm using this kind of generation with my http framework (kwiscale)

To declare HEAD and CONTENT templates is required because I want to be able to not redeclare one of them in children templates. And this is ok, it works and I'm glade :)

The question is: Try to insert some content inside T1->CONTENT template. You will see that the compilation breaks. Go template module says that there are multiple definition of templates.

But without content in T1 definition, everything is ok.

The question is: Is it normal that defining empty templates works ? if no: please don't treat this as a bug, it saves my life.

Second question:  I'd like to set "default" content, and let user redefine CONTENT and/or HEAD template. Any possibility ?

Thanks a lot

aka...@gmail.com

unread,
Jul 15, 2013, 8:06:18 AM7/15/13
to golan...@googlegroups.com
On Monday, July 15, 2013 6:05:26 AM UTC-5, Metal3d wrote:
The question is: Is it normal that defining empty templates works ? if no: please don't treat this as a bug, it saves my life.

Yes, it is normal (I think). From http://golang.org/pkg/text/template/#Template.Parse :
" It is an error if a resulting template is non-empty (contains content other than template definitions) and would replace a non-empty template with the same name." 

Carlos Castillo

unread,
Jul 15, 2013, 9:39:40 AM7/15/13
to golan...@googlegroups.com
I didn't know that empty templates worked this way; if it is intentional it could be meant to solve chicken vs. egg problems, similar to function prototypes or type declarations in C.

Default contents can be worked around, if you pass a variable to a template that is mutable by that template: http://play.golang.org/p/l0uwzgC_Qf . Alternatively you could use template functions to do the same thing. The downside is that the sub-templates you do define need to do something extra to "turn off" the default behaviour, as opposed to having a default sub-template to "turn on" the behaviour.

Metal3d

unread,
Jul 15, 2013, 3:18:32 PM7/15/13
to golan...@googlegroups.com
@Carlos Castillo: I checked your example. Thanks a lot.
But as I can see, we must send "Head" and "Content" for the whole context definition. What I'm expecting is to let this part be played only by templates. BTW, I didn't know the trick, thanks a lot !
For now, the restriction to not "redefine a template that is filled" is not a big problem, but in a near futur I will use your snippet. So really, thanks.

@Matt k : I probably forgotten those lines :) Thanks to point me the explanation :)
Reply all
Reply to author
Forward
0 new messages