Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yfzhou0904 opened this issue Oct 10, 2022 · 1 comment
Open

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

yfzhou0904 opened this issue Oct 10, 2022 · 1 comment

Comments

@yfzhou0904
Copy link

yfzhou0904 commented Oct 10, 2022

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

@NikolaLohinski
Copy link

Fixed in NikolaLohinski/gonja@aba64fe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants