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

"if not" condition not working properly #209

Open
dgofman opened this issue Jun 18, 2019 · 2 comments
Open

"if not" condition not working properly #209

dgofman opened this issue Jun 18, 2019 · 2 comments

Comments

@dgofman
Copy link

dgofman commented Jun 18, 2019

Open file: pongo2/template_tests/if.tpl and add two conditions after this line:
{% if "Text" in complex.post %}text field in complex.post{% endif %}

Hello string {% if not "Hello" in complex.post.Text %}is not{% else %}is{% endif %} in complex.post.Text
World string {% if not "World" in complex.post.Text %}is not{% else %}is{% endif %} in complex.post.Text

In pongo2_template_test.go the "complex.post.Text" = <h2>Hello!</h2><p>Welcome...

So, we are expecting the "World" does not exist in the string we should get two different results, However result are:

 Hello string is in complex.post.Text
 World string is in complex.post.Text
@dgofman
Copy link
Author

dgofman commented Jun 18, 2019

Open the file parser_expression.go, the problem in "result = result.Negate()". It may work for Boolean types but definitely not for String.

func (expr *simpleExpression) Evaluate(ctx *ExecutionContext) (*Value, *Error) {
	t1, err := expr.term1.Evaluate(ctx)
	if err != nil {
		return nil, err
	}
	result := t1

	if expr.negate {
		result = result.Negate()
	}

Such as common function for backward compatibility I implemented the fix in that function:
"func (expr *relationalExpression) Evaluate(ctx *ExecutionContext) (*Value, *Error) {"

                   case "in":
			val, ok := expr.expr1.(*simpleExpression)
			if ok && val.negate {
				t1, err := val.term1.Evaluate(ctx)
				if err == nil {
					return AsValue(!v2.Contains(t1)), nil
				}
			}
			return AsValue(v2.Contains(v1)), nil

@dgofman
Copy link
Author

dgofman commented Jun 18, 2019

I opened a pull request

#210

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

No branches or pull requests

2 participants