Skip to content

Doesn't support Jinja's for...else syntax #10

@yfzhou0904

Description

@yfzhou0904
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.

https://jinja.palletsprojects.com/en/3.1.x/templates/#for

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions