You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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|format syntax in jinja2cpp behaves like str.format syntax in Python jinja2. That is to say that jinja2cpp uses {} string formatting for str|format when it should instead use %s string formatting (per spec / Python parity).
str.format syntax is not supported in jinja2cpp. This should have the {} string formatting behavior, so existing str|format implementation in jinja2cpp could perhaps largely be reused / repurposed to support this.
The text was updated successfully, but these errors were encountered:
tjsmith-meta
changed the title
Fix "str|format" string formatting to match spec (%s formatting, not {} formatting)
Fix "str|format" string formatting to match spec (%s formatting, not {} formatting), also support "str.format' syntax
Sep 26, 2024
tjsmith-meta
changed the title
Fix "str|format" string formatting to match spec (%s formatting, not {} formatting), also support "str.format' syntax
Fix "str|format" string formatting to match spec (%s formatting, not {} formatting), also support "str.format" syntax
Sep 26, 2024
The jinja2 spec alludes to two flavors of format --
str.format
andstr|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), whereasstr|format
appears to support % string formatting (like printf). Here's a quick mapping / handy guide as I understand it."hello %"|format("world")
-> Python `"hello %" % "world""hello {}".format("world")
-> Python"hello {}".format("world")
Here's a template with corresponding Python jinja2 output.
And a similar template with its corresponding jinja2cpp output.
From these examples, we can see a couple key differences between jinja2cpp and Python jinja2.
str|format
syntax in jinja2cpp behaves likestr.format
syntax in Python jinja2. That is to say that jinja2cpp uses {} string formatting forstr|format
when it should instead use %s string formatting (per spec / Python parity).str.format
syntax is not supported in jinja2cpp. This should have the {} string formatting behavior, so existingstr|format
implementation in jinja2cpp could perhaps largely be reused / repurposed to support this.The text was updated successfully, but these errors were encountered: