FAQ
I am trying to print out an number inside a html/template.

{{range $index, $p := .}}
No. {{$index+1}}:{{$p}}
{{end}}

The intent was to print out this
No. 1 xxx
No. 2 yyy
No. 3 zzz

However, {{$index+1}} does not work.

Is there an easy way to fix this?

--

Search Discussions

  • Rémy Oudompheng at Oct 27, 2012 at 12:49 pm

    On 2012/10/27 ChrisLu wrote:
    I am trying to print out an number inside a html/template.

    {{range $index, $p := .}}
    No. {{$index+1}}:{{$p}}
    {{end}}

    The intent was to print out this
    No. 1 xxx
    No. 2 yyy
    No. 3 zzz

    However, {{$index+1}} does not work.

    Is there an easy way to fix this?
    Write a plus(a, b int) int or a plusOne(int)int function and attach it
    to your template.

    Rémy.

    --
  • ChrisLu at Oct 28, 2012 at 12:19 am
    Thanks! This works. However, I did not expect such simple expressions,
    {{$x+1}}, are not included in the default template system. Is there any
    other template language for Go already supporting these?

    Chris
    On Saturday, October 27, 2012 3:02:52 AM UTC-7, Rémy Oudompheng wrote:
    On 2012/10/27 ChrisLu <chri...@gmail.com <javascript:>> wrote:
    I am trying to print out an number inside a html/template.

    {{range $index, $p := .}}
    No. {{$index+1}}:{{$p}}
    {{end}}

    The intent was to print out this
    No. 1 xxx
    No. 2 yyy
    No. 3 zzz

    However, {{$index+1}} does not work.

    Is there an easy way to fix this?
    Write a plus(a, b int) int or a plusOne(int)int function and attach it
    to your template.

    Rémy.
    --
  • Jesse McNelis at Oct 28, 2012 at 3:03 am

    On Sun, Oct 28, 2012 at 11:19 AM, ChrisLu wrote:
    Thanks! This works. However, I did not expect such simple expressions,
    {{$x+1}}, are not included in the default template system. Is there any
    other template language for Go already supporting these?
    Go is already a programming language, no need to implement another one
    just for text/template.
    In general it's bad form to be going calculations in your templates anyway.






    --
    =====================
    http://jessta.id.au

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 27, '12 at 10:48a
activeOct 28, '12 at 3:03a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase