FAQ
Hi,

What are the reasons why it is not possible to assign a new value to an
already initiated variable in the text/template package?

Example where it is needed:

Let's say I have a list of people (json):

[
     {"name": "ANisus", "sex":"M"},
     {"name": "Sofia", "sex":"F"},
     {"name": "Anna", "sex":"F"}
]

Using the template package, I want to have this output:

Females:
Sofia
Anna

But the header "Females:" should only show in case there are actually any
person with sex set to F.
If I could reassign a value, I would do the following:

{{$hasFemales := 0}}
{{range .}}{{if eq .sex "F"}}{{$hasFemales = 1}}{{end}}{{end}}
{{if $hasFemale}}Female:{{end}}

Example: http://play.golang.org/p/T-Ekx7n9YQ

/ANisus

--
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

  • Jesse McNelis at Oct 10, 2013 at 10:15 am

    On 10/10/2013 8:54 PM, "Accipiter Nisus" wrote:
    Hi,

    What are the reasons why it is not possible to assign a new value to an
    already initiated variable in the text/template package?
    >
    Templates aren't the place for doing complex logic. Do it before you pass
    the data to the template.
    {{$hasFemales := 0}}
    {{range .}}{{if eq .sex "F"}}{{$hasFemales = 1}}{{end}}{{end}}
    {{if $hasFemale}}Female:{{end}}

    Example: http://play.golang.org/p/T-Ekx7n9YQ
    You can easily count the females before you pass the slice to the template
    and pass that in too.

    --
    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.
  • Accipiter Nisus at Oct 10, 2013 at 12:18 pm

    Templates aren't the place for doing complex logic. Do it before you pass
    the data to the template.
    Whether or not assigning a new value to a variable is considered complex
    logic might be up for discussion.

    When the template package is used in a generic context (eg. Executing an
    arbitrary template file with an arbitrary json file), you might not have
    the luxury of doing precalculations.

    /ANisus

    --
    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.
  • Steven Blenkinsop at Oct 11, 2013 at 2:52 am
    You could do something like this if you had to:
    http://play.golang.org/p/SufZdsx-1v

    On Thu, Oct 10, 2013 at 8:18 AM, Accipiter Nisus wrote:


    Templates aren't the place for doing complex logic. Do it before you pass
    the data to the template.
    Whether or not assigning a new value to a variable is considered complex
    logic might be up for discussion.

    When the template package is used in a generic context (eg. Executing an
    arbitrary template file with an arbitrary json file), you might not have
    the luxury of doing precalculations.

    /ANisus

    --
    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.
    --
    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.
  • Ibrahim M. Ghazal at Oct 10, 2013 at 10:47 am
    Actually, you *can* assign new values to variables:
    http://play.golang.org/p/GzXhsDFpom

    package main

    import (
      "os"
    "text/template"
    )

    func main() {
    const tmpl = `
    {{$foo := 0}}
    $foo is {{$foo}}
    {{$foo := 1}}
    Now $foo is {{$foo}}
    `
    t := template.Must(template.New("tmpl").Parse(tmpl))
      err := t.Execute(os.Stdout, nil)
    if err != nil {
    panic(err)
      }
    }


    Output:

    $foo is 0

    Now $foo is 1

    --
    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.
  • Accipiter Nisus at Oct 10, 2013 at 11:24 am
    Well, what do you know.

    But, it only works within the same scope. I think it is not so much
    reassigning a value as replacing the previous variable in the scope with a
    new one.
    In my playground code, it doesn't work. $hasFemale in the outer scope
    remains unchanged.

    On Thursday, 10 October 2013 12:47:13 UTC+2, Ibrahim M. Ghazal wrote:

    Actually, you *can* assign new values to variables:
    http://play.golang.org/p/GzXhsDFpom

    package main

    import (
    "os"
    "text/template"
    )

    func main() {
    const tmpl = `
    {{$foo := 0}}
    $foo is {{$foo}}
    {{$foo := 1}}
    Now $foo is {{$foo}}
    `
    t := template.Must(template.New("tmpl").Parse(tmpl))
    err := t.Execute(os.Stdout, nil)
    if err != nil {
    panic(err)
    }
    }


    Output:

    $foo is 0

    Now $foo is 1
    --
    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.
  • Sorenhoyerkristensen at Mar 11, 2015 at 12:33 pm
    I don't think something like this is "complex logic" and when developing a
    generic application like a CMS, this functionality would be very handy to
    have, like we can with Razor.

    This is the biggest (and only) problems I have with Go. I simply do not
    understand why this functionality has not been implemented yet.

    Den torsdag den 10. oktober 2013 kl. 11.54.31 UTC+2 skrev Accipiter Nisus:
    Hi,

    What are the reasons why it is not possible to assign a new value to an
    already initiated variable in the text/template package?

    Example where it is needed:

    Let's say I have a list of people (json):

    [
    {"name": "ANisus", "sex":"M"},
    {"name": "Sofia", "sex":"F"},
    {"name": "Anna", "sex":"F"}
    ]

    Using the template package, I want to have this output:

    Females:
    Sofia
    Anna

    But the header "Females:" should only show in case there are actually any
    person with sex set to F.
    If I could reassign a value, I would do the following:

    {{$hasFemales := 0}}
    {{range .}}{{if eq .sex "F"}}{{$hasFemales = 1}}{{end}}{{end}}
    {{if $hasFemale}}Female:{{end}}

    Example: http://play.golang.org/p/T-Ekx7n9YQ

    /ANisus
    --
    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 10, '13 at 9:54a
activeMar 11, '15 at 12:33p
posts7
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase