Fix "str|format" string formatting to match spec (%s formatting, not {} formatting), also support "str.format" syntax
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 38/100
Research direction
Start by locating the existing implementation of the str|format filter and the parser or evaluator entry point for string method calls. Compare both forms against the template examples and Python Jinja2 behavior in the issue. Done means % formatting works for str|format, str.format supports {} formatting, and the shown outputs match.
Written by the indexing model from the issue text.
Description
The jinja2 spec alludes to two flavors of format -- str.format and str|format -- that seemingly have / support different string formatting syntaxes.
https://jinja.palletsprojects.com/en/2.10.x/templates/
The str.format syntax appears to support standard {} string formatting (like fmt::format), whereas str|format appears to support % string formatting (like printf). Here's a quick mapping / handy guide as I understand it.
- jinja2
"hello %"|format("world")-> Python `"hello %" % "world" - jinja2
"hello {}".format("world")-> Python"hello {}".format("world")
Here's a template with corresponding Python jinja2 output.
# template; note that the "2 {}" case fails to compile if arg is passed
{{ "1 %s"|format(0) }}
{{ "2 {}"|format() }}
{{ "3 %s".format(0) }}
{{ "4 {}".format(0) }}
# Python jinja2 output
1 0
2 {}
3 %s
4 0
And a similar template with its corresponding jinja2cpp output.
# template; note that 3/4 are missing because `.format` syntax not currently supported in jinja2cpp
{{ "3 %s"|format(0) }}
{{ "4 {}"|format(0) }}
# jinja2cpp output
3 %s
4 0
From these examples, we can see a couple key differences between jinja2cpp and Python jinja2.
str|formatsyntax in jinja2cpp behaves likestr.formatsyntax in Python jinja2. That is to say that jinja2cpp uses {} string formatting forstr|formatwhen it should instead use %s string formatting (per spec / Python parity).str.formatsyntax is not supported in jinja2cpp. This should have the {} string formatting behavior, so existingstr|formatimplementation in jinja2cpp could perhaps largely be reused / repurposed to support this.
- Dominant language
- C++
- Stars
- 602
- Forks
- 115
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from jinja2cpp/Jinja2Cpp
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 4/5 3-5 days Newbie friendliness 65/100
-
Difficulty 3/5 1-2 days Newbie friendliness 74/100
-
Difficulty 3/5 1-2 days Newbie friendliness 70/100
All issues in jinja2cpp/Jinja2Cpp
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
mpfaffenberger/privateer_reimagined#658 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
microsoft/onnxruntime#33018 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
AXERA-TECH/ax-llm#81 ·
-
enhancement
Difficulty 2/5 Half a day Newbie friendliness 78/100
ros-industrial/ros2_canopen#448 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Maintainers usually reply within 1 day