eknkc / amber

Amber is an elegant templating engine for Go Programming Language, inspired from HAML and Jade
MIT License
914 stars 63 forks source link

iteration loop doesn't recognize page level data #45

Open stephenhu opened 8 years ago

stephenhu commented 8 years ago

if i pass a struct into an amber template, i can't seem to access the variables inside of an iteration block:

let's say i have this struct:

type Foo struct { ID string OK bool Bar []string }

// initialized here f = Foo{ID: "abc", OK: true, Bar: make([]string){"ddd", "eee"}}

passed into the template.Execute function:

template.Execute(w, f)

i can do the following without any issues: h3 #{ID} // h3 abc

but if i do this:

each $i in Bar h3 #{$i} // works fine h3 #{ID} // causes an Amber Error <>: Unable to parse expression

exact error is: Amber Error in <static/templates/viewteam.amber>: Unable to parse expression. - Line: 62, Column: 21, Length: 2

stephenhu commented 8 years ago

there's a workaround which is to assign the page level variable to a variable:

e.g.

$id = ID

each $i in Bar h3 #{$i} // works fine h3 #{$id} // this works

stephenhu commented 8 years ago

i would say this is a fairly serious limitation, seems nested loops or nested maps also have the same issue and the work around is not very apparent to me at the moment.