Recursion in text/template

1,585 views
Skip to first unread message

Lucio

unread,
Aug 4, 2012, 12:25:30 PM8/4/12
to golan...@googlegroups.com
I didn't find mention of it in the package documentation, but a small experiment showed that it is possible to apply recursion to templates successfully.  Conspicuous by its absence, however, is a small helpful feature: the ability to name the recursive template in its invocation(s).  My choice would be a reserved variable such as $self, say, but others may have a better idea.  Multilevel recursion, however, isn't going to gain much from it, there the designer is going to have to know the names of the templates involved.

As an aside, does anyone have a suggestion on how to place commas in the inner spaces (only) in a list of strings?  I want "a, b, c" rather than "a, b, c," and in my inexperience, I can't see an easy way to do it (I resorted to making the comma part of the string contents in all but the last element in the list).

Lucio.

Martin Schnabel

unread,
Aug 4, 2012, 1:18:58 PM8/4/12
to golan...@googlegroups.com
> As an aside, does anyone have a suggestion on how to place commas in the
> inner spaces (only) in a list of strings? I want "a, b, c" rather than
> "a, b, c," and in my inexperience, I can't see an easy way to do it (I
> resorted to making the comma part of the string contents in all but the
> last element in the list).

you can use the index to check for the first element. like this:
http://play.golang.org/p/Fqn84WDRkj

Sathish VJ

unread,
Aug 5, 2012, 11:10:22 AM8/5/12
to golan...@googlegroups.com
Hello Lucio, could you please give me an example of what you mean by recursive templates and also naming them? 

Russ Cox

unread,
Aug 9, 2012, 7:42:38 PM8/9/12
to Lucio, golan...@googlegroups.com
On Sat, Aug 4, 2012 at 12:25 PM, Lucio <lucio...@gmail.com> wrote:
> I didn't find mention of it in the package documentation, but a small
> experiment showed that it is possible to apply recursion to templates
> successfully. Conspicuous by its absence, however, is a small helpful
> feature: the ability to name the recursive template in its invocation(s).
> My choice would be a reserved variable such as $self, say, but others may
> have a better idea. Multilevel recursion, however, isn't going to gain much
> from it, there the designer is going to have to know the names of the
> templates involved.

The full Go language has no such concept. I'm not sure why the
restricted template language would need it.

Russ

Lucio De Re

unread,
Aug 10, 2012, 1:51:28 AM8/10/12
to Russ Cox, golan...@googlegroups.com
The problem I'm trying to address is that within a given template one
currently needs to hard code the name by which the template itself is
being invoked, which is a factor beyond the control of the template
writer.

The equivalent in Go itself would be having to guess the name of a
closure within a closure. As closures are a bit of a new concept to
me, I'm not sure what actually happens there :-(

The simplest example I could come up with lies at the URL below.

http://play.golang.org/p/QQu77gkbl6

Lucio.
Reply all
Reply to author
Forward
0 new messages