encountered same problem, as it seems still an open issue, cannot change 
value of an existing variable inside of loop/if in template 
https://github.com/golang/go/issues/10608
...

      {{$deprecated := false}}      {{range $depre_audit := $depre_audits}}     
 {{if eq $audit_name $depre_audit}}      {{$deprecated := true}}   ------> this 
line doesn't work beyond end      <span 
class="audit_name_chart">{{$audit_name}} - deprecated</span>      {{end}}      
{{end}}      {{if not $deprecated}}      <span 
class="audit_name_chart">{{$audit_name}}</span>      {{end}}





On Thursday, October 10, 2013 at 8:54:31 PM UTC+11, 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?
>
> 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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to