You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func TestJinja(t *testing.T) {
jsonStr := `{"search": "yeno"}`
c := map[string]interface{}{}
err := json.Unmarshal([]byte(jsonStr), &c)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
tmpl := `
{%- set keywords = ['yes', 'hello'] -%}
{%- for w in keywords if w in search -%}
{%- if loop.first -%}
match
{%- endif -%}
{%- else -%}
no match
{%- endfor -%}
`
tpl, err := gonja.FromString(tmpl)
if err != nil {
return
}
out, err := tpl.Execute(c)
if err != nil {
fmt.Println(err.Error())
t.FailNow()
}
fmt.Println("output:", out)
}
The above code tests whether an input string contains any string (as substring) in a list predefined inside a Jinja template. However, going into the else branch produces an empty output whereas "no match" is expected. The "match" case works though.
The above code tests whether an input string contains any string (as substring) in a list predefined inside a Jinja template. However, going into the else branch produces an empty output whereas "no match" is expected. The "match" case works though.
https://jinja.palletsprojects.com/en/3.1.x/templates/#for
The text was updated successfully, but these errors were encountered: