FAQ
Hi all,

I have the following HTML that I'd like to generate using Go's template
package.

http://bpaste.net/show/PkBal2SZzFr9HjXNnpIg/

I have a slice of 6-12 structs that I need to range over, but I can't
figure out how to approach it. I've used Jinja and Haml in the past, they
support arbitrary expressions which things a smidge easier. For example, I
can an inline modulus operator to calculate alternating rows {{ x%2 ==0 ?
"blue" : "green" }}.

Should create a method on my struct for something like alternating row
colors?

{{range $x, $foo := .}}
   {{$foo.IsEven $x}}
{{end}}

I'm using Go 1.1

Thanks,
Greg


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Martin Schnabel at Nov 15, 2013 at 10:54 am

    On 11/15/2013 04:27 AM, Greg Ing wrote:
    Hi all,

    I have the following HTML that I'd like to generate using Go's template
    package.

    http://bpaste.net/show/PkBal2SZzFr9HjXNnpIg/

    I have a slice of 6-12 structs that I need to range over, but I can't
    figure out how to approach it. I've used Jinja and Haml in the past,
    they support arbitrary expressions which things a smidge easier. For
    example, I can an inline modulus operator to calculate alternating rows
    {{ x%2 ==0 ? "blue" : "green" }}.

    Should create a method on my struct for something like alternating row
    colors?

    {{range $x, $foo := .}}
    {{$foo.IsEven $x}}
    {{end}}
    you can also pass a map of functions to the template:
    http://play.golang.org/p/OJzOip2vrR

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Carlos Castillo at Nov 15, 2013 at 12:15 pm
    Or you could pass a function which returns an infinite pattern of
    (true,false,true,false...): http://play.golang.org/p/MHcouwBF5E

    Or a value that behaves the similarly through one of its
    methods: http://play.golang.org/p/iom2O0smu3
    On Friday, November 15, 2013 2:54:44 AM UTC-8, mb0 wrote:
    On 11/15/2013 04:27 AM, Greg Ing wrote:
    Hi all,

    I have the following HTML that I'd like to generate using Go's template
    package.

    http://bpaste.net/show/PkBal2SZzFr9HjXNnpIg/

    I have a slice of 6-12 structs that I need to range over, but I can't
    figure out how to approach it. I've used Jinja and Haml in the past,
    they support arbitrary expressions which things a smidge easier. For
    example, I can an inline modulus operator to calculate alternating rows
    {{ x%2 ==0 ? "blue" : "green" }}.

    Should create a method on my struct for something like alternating row
    colors?

    {{range $x, $foo := .}}
    {{$foo.IsEven $x}}
    {{end}}
    you can also pass a map of functions to the template:
    http://play.golang.org/p/OJzOip2vrR
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Greg Ing at Nov 15, 2013 at 5:59 pm

    On Friday, November 15, 2013 7:15:02 AM UTC-5, Carlos Castillo wrote:
    Or you could pass a function which returns an infinite pattern of
    (true,false,true,false...): http://play.golang.org/p/MHcouwBF5E

    Or a value that behaves the similarly through one of its methods:
    http://play.golang.org/p/iom2O0smu3
    On Friday, November 15, 2013 2:54:44 AM UTC-8, mb0 wrote:
    On 11/15/2013 04:27 AM, Greg Ing wrote:
    Hi all,

    I have the following HTML that I'd like to generate using Go's template
    package.

    http://bpaste.net/show/PkBal2SZzFr9HjXNnpIg/

    I have a slice of 6-12 structs that I need to range over, but I can't
    figure out how to approach it. I've used Jinja and Haml in the past,
    they support arbitrary expressions which things a smidge easier. For
    example, I can an inline modulus operator to calculate alternating rows
    {{ x%2 ==0 ? "blue" : "green" }}.

    Should create a method on my struct for something like alternating row
    colors?

    {{range $x, $foo := .}}
    {{$foo.IsEven $x}}
    {{end}}
    you can also pass a map of functions to the template:
    http://play.golang.org/p/OJzOip2vrR
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Greg Ing at Nov 15, 2013 at 5:59 pm
    Thanks, I didn't realize passing funcs was that easy. For future reference
    to others http://golang.org/pkg/text/template/#example_Template_func

    On Friday, November 15, 2013 5:54:44 AM UTC-5, mb0 wrote:
    On 11/15/2013 04:27 AM, Greg Ing wrote:
    Hi all,

    I have the following HTML that I'd like to generate using Go's template
    package.

    http://bpaste.net/show/PkBal2SZzFr9HjXNnpIg/

    I have a slice of 6-12 structs that I need to range over, but I can't
    figure out how to approach it. I've used Jinja and Haml in the past,
    they support arbitrary expressions which things a smidge easier. For
    example, I can an inline modulus operator to calculate alternating rows
    {{ x%2 ==0 ? "blue" : "green" }}.

    Should create a method on my struct for something like alternating row
    colors?

    {{range $x, $foo := .}}
    {{$foo.IsEven $x}}
    {{end}}
    you can also pass a map of functions to the template:
    http://play.golang.org/p/OJzOip2vrR
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Egon at Nov 15, 2013 at 1:16 pm
    I would simply recommend using CSS for the alternate coloring. e.g.

    tr:nth-child(odd){ background:#ff0000; }
    tr:nth-child(even){ background:#0000ff; }

    Although it doesn't work everywhere (http://caniuse.com/#search=nth-child),
    it will make things much easier.

    + egon
    On Friday, November 15, 2013 5:27:40 AM UTC+2, Greg Ing wrote:

    Hi all,

    I have the following HTML that I'd like to generate using Go's template
    package.

    http://bpaste.net/show/PkBal2SZzFr9HjXNnpIg/

    I have a slice of 6-12 structs that I need to range over, but I can't
    figure out how to approach it. I've used Jinja and Haml in the past, they
    support arbitrary expressions which things a smidge easier. For example, I
    can an inline modulus operator to calculate alternating rows {{ x%2 ==0 ?
    "blue" : "green" }}.

    Should create a method on my struct for something like alternating row
    colors?

    {{range $x, $foo := .}}
    {{$foo.IsEven $x}}
    {{end}}

    I'm using Go 1.1

    Thanks,
    Greg

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Greg Ing at Nov 15, 2013 at 5:46 pm
    Unfortunately, I can't change the HTML to use CSS but thanks for the tip.
    On Friday, November 15, 2013 8:16:14 AM UTC-5, egon wrote:

    I would simply recommend using CSS for the alternate coloring. e.g.

    tr:nth-child(odd){ background:#ff0000; }
    tr:nth-child(even){ background:#0000ff; }

    Although it doesn't work everywhere (http://caniuse.com/#search=nth-child),
    it will make things much easier.

    + egon
    On Friday, November 15, 2013 5:27:40 AM UTC+2, Greg Ing wrote:

    Hi all,

    I have the following HTML that I'd like to generate using Go's template
    package.

    http://bpaste.net/show/PkBal2SZzFr9HjXNnpIg/

    I have a slice of 6-12 structs that I need to range over, but I can't
    figure out how to approach it. I've used Jinja and Haml in the past, they
    support arbitrary expressions which things a smidge easier. For example, I
    can an inline modulus operator to calculate alternating rows {{ x%2 ==0 ?
    "blue" : "green" }}.

    Should create a method on my struct for something like alternating row
    colors?

    {{range $x, $foo := .}}
    {{$foo.IsEven $x}}
    {{end}}

    I'm using Go 1.1

    Thanks,
    Greg

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 15, '13 at 3:27a
activeNov 15, '13 at 5:59p
posts7
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase