Arithmetic operations inside of html/template

6,903 views
Skip to first unread message

Francesc Campoy Flores

unread,
Oct 19, 2012, 2:38:45 PM10/19/12
to golan...@googlegroups.com
Hi,

I'd like to print the length of an array + 1 in a template, but it seems that's not possible, or I'm missing some line in the specs?

What I'm trying to do, and which is obviously wrong: http://play.golang.org/p/1HrXbu9ujj

Anyone has an idea on how to do this if possible?

--
Francesc

Joubin Houshyar

unread,
Oct 19, 2012, 2:58:07 PM10/19/12
to golan...@googlegroups.com


On Friday, October 19, 2012 2:39:18 PM UTC-4, Francesc Campoy Flores wrote:
Hi,

I'd like to print the length of an array + 1 in a template, but it seems that's not possible, or I'm missing some line in the specs?

You can bind funcs to the template.  See spec below and (a bit lower) an example.

Francesc Campoy Flores

unread,
Oct 19, 2012, 3:47:48 PM10/19/12
to Joubin Houshyar, golan...@googlegroups.com
Thanks, I saw that, but I feel a bit weird adding so much boilerplate for adding one to a number :-/

--
 
 



--
--
Francesc

Joubin Houshyar

unread,
Oct 19, 2012, 5:18:56 PM10/19/12
to golan...@googlegroups.com, Joubin Houshyar


On Friday, October 19, 2012 3:48:34 PM UTC-4, Francesc Campoy Flores wrote:
Thanks, I saw that, but I feel a bit weird adding so much boilerplate for adding one to a number :-/

It's not that bad. 

Rob Pike

unread,
Oct 19, 2012, 5:27:30 PM10/19/12
to Joubin Houshyar, golan...@googlegroups.com
It's a slippery slope thing. Sure, +1 is easy but then you want *2 and
then <<3 and then switch and then closures and so on. Unlike with
templates in other languages, I deliberately chose not to provide a
full programming language, and ask the programmer to use the one
that's already there.

Functions are underappreciated. In general, not just in templates.

-rob

Joubin Houshyar

unread,
Oct 19, 2012, 6:05:54 PM10/19/12
to Rob Pike, golan...@googlegroups.com
Possibility remains to beef up the builtins, and/or export a select subset (e.g. truth -> Truth) as helpers at the package level.  The example just posted is not doing any type checking, etc. as it should.   I think end users would benefit from a refactored length to call a Container (akin to truth) that can be used to build up their specific required funcs.  "Batteries included".
 

-rob

Steven Blenkinsop

unread,
Oct 19, 2012, 7:38:10 PM10/19/12
to Joubin Houshyar, golan...@googlegroups.com
On Friday, October 19, 2012, Joubin Houshyar wrote:


On Friday, October 19, 2012 3:48:34 PM UTC-4, Francesc Campoy Flores wrote:
Thanks, I saw that, but I feel a bit weird adding so much boilerplate for adding one to a number :-/

It's not that bad. 


It's not even that bad. You can just return the number:

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

You can even use a simpler, more general function which you can then compose with other functions to get the same result:

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

Francesc Campoy Flores

unread,
Oct 19, 2012, 7:42:49 PM10/19/12
to Steven Blenkinsop, Joubin Houshyar, golan...@googlegroups.com
I actually agree with minimizing the power of the expressions you could use on templates. I don't want to end up writing php-like templates.

Thanks for your solutions, I actually ended up creating a new method on the object I was passing generating the value I wanted, which is pretty similar to what you proposed, but I avoid adding functions to the map.

Thanks! 


--
 
 



--
--
Francesc

Reply all
Reply to author
Forward
0 new messages