jinja2cpp / jinja2cpp/Jinja2Cpp

Length filter does not work directly on strings

Open
#273 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
600
Forks
114
PR merge metrics
No merged PRs in 30d

Description

In jinja2, you can apply the length filter directly on strings like: `{{ my_string | length }}`, this will produce an empty value in jinja2Cpp.

The fix seems to be as simple as changing [filters.cpp:457](https://github.com/jinja2cpp/Jinja2Cpp/blob/master/src/filters.cpp#L457)
```diff
diff --git a/src/filters.cpp b/src/filters.cpp
--- a/src/filters.cpp
+++ b/src/filters.cpp
@@ -454,7 +454,7 @@ InternalValue SequenceAccessor::Filter(const InternalValue& baseVal, RenderConte
InternalValue result;

bool isConverted = false;
- ListAdapter list = ConvertToList(baseVal, isConverted);
+ ListAdapter list = ConvertToList(baseVal, isConverted, false);

if (!isConverted)
return result;
```

And adding a [test](https://github.com/jinja2cpp/Jinja2Cpp/blob/master/test/filters_test.cpp#L163):
```diff
diff --git a/test/filters_test.cpp b/test/filters_test.cpp
--- a/test/filters_test.cpp
+++ b/test/filters_test.cpp
@@ -161,6 +161,7 @@ INSTANTIATE_TEST_SUITE_P(Length, FilterGenericTest, ::testing::Values(
InputOutputPair{"intValue | length", ""},
InputOutputPair{"intList | length", "10"},
InputOutputPair{"stringValue | list | length", "4"},
+ InputOutputPair{"stringValue | length", "4"},
InputOutputPair{"reflectedIntVector | length", "10"}
));
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.