jinja2cpp / jinja2cpp/Jinja2Cpp
Support 'is not _' expression syntax
- Dominant language
- C++
- Stars
- 600
- Forks
- 114
- PR merge metrics
- No merged PRs in 30d
Description
Here's the example template that works in Python jinja2 -- 'is not defined'.
```
{% if foo is not defined %}
not defined
{% endif %}
{% set foo = False %}
{% if foo is not defined %}
{% set foo = True %}
{% endif %}
{{ foo }}
```
I realize there is an 'undefined' operator which is already supported, but 'not defined' appears to be a valid formulation as well.
Here's the change I have for reference that seems to do the trick for me.
```
diff --git a/jinja2cpp/src/expression_parser.cpp b/jinja2cpp/src/expression_parser.cpp
--- a/jinja2cpp/src/expression_parser.cpp
+++ b/jinja2cpp/src/expression_parser.cpp
@@ -151,7 +151,14 @@
break;
case Keyword::Is:
{
+ bool wrapWithNot = false;
+
Token nextTok = lexer.NextToken();
+ if (lexer.GetAsKeyword(nextTok) == Keyword::LogicalNot) {
+ wrapWithNot = true;
+ nextTok = lexer.NextToken();
+ }
+
if (nextTok != Token::Identifier)
return MakeParseError(ErrorCode::ExpectedIdentifier, nextTok);
@@ -164,7 +171,12 @@
if (!params)
return params.get_unexpected();
- return std::make_shared(*left, std::move(name), std::move(*params));
+ auto isExpr = std::make_shared(*left, std::move(name), std::move(*params));
+ if (wrapWithNot) {
+ return std::make_shared(UnaryExpression::Operation::LogicalNot, isExpr);
+ } else {
+ return isExpr;
+ }
}
default:
lexer.ReturnToken();
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.