Skip to content
Myr

ignore trim_blocks using '+%}'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Amy · committed by David Lord · parent 9b718ed · Browse files · 4 files+136 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+136 added−5 removed

  • tests:+118 added−0 removed
  • other:+18 added−5 removed

4 files+136 added−5 removed

modified:docs/templates.rst+9 added
228 unmodified lines
229 <div>230 {%+ if something %}yay{% endif %}231 </div>232233Similarly, you can manually disable the ``trim_blocks`` behavior by234putting a plus sign (``+``) at the end of a block::235236 <div>237 {% if something +%}238 yay239 {% endif %}240 </div>241242You can also strip whitespace in templates by hand. If you add a minus243sign (``-``) to the start or end of a block (e.g. a :ref:`for-loop` tag), a244comment, or a variable expression, the whitespaces before or after245that block will be removed::
1583 unmodified lines
modified:src/jinja2/lexer.py+8 added-4 removed
507 unmodified lines
508 # comments509 TOKEN_COMMENT_BEGIN: [510 (511 c(512 fr"(.*?)((?:\-{comment_end_re}\s*"513 fr"|{comment_end_re}){block_suffix_re})"512 fr"(.*?)((?:\+{comment_end_re}|\-{comment_end_re}\s*"513 fr"|{comment_end_re}{block_suffix_re}))"514 ),515 (TOKEN_COMMENT, TOKEN_COMMENT_END),516 "#pop",517 ),
1 unmodified line
519 ],520 # blocks521 TOKEN_BLOCK_BEGIN: [522 (523 c(fr"(?:\-{block_end_re}\s*|{block_end_re}){block_suffix_re}"),523 c(524 fr"(?:\+{block_end_re}|\-{block_end_re}\s*"525 fr"|{block_end_re}{block_suffix_re})"526 ),527 TOKEN_BLOCK_END,528 "#pop",529 ),530 ]
11 unmodified lines
542 TOKEN_RAW_BEGIN: [543 (544 c(545 fr"(.*?)((?:{block_start_re}(\-|\+|))\s*endraw\s*"543 fr"(?:\-{block_end_re}\s*|{block_end_re}{block_suffix_re}))"546 fr"(?:\+{block_end_re}|\-{block_end_re}\s*"547 fr"|{block_end_re}{block_suffix_re}))"548 ),549 OptionalLStrip(TOKEN_DATA, TOKEN_RAW_END),550 "#pop",551 ),
250 unmodified lines
modified:tests/test_lexnparse.py+118 added
901 unmodified lines
902 ${item}903<!--- endfor -->"""904 )905 assert tmpl.render(seq=range(5)) == "01234"906907908class TestTrimBlocks:909 def test_trim(self, env):910 env = Environment(trim_blocks=True, lstrip_blocks=False)911 tmpl = env.from_string(" {% if True %}\n {% endif %}")912 assert tmpl.render() == " "913914 def test_no_trim(self, env):915 env = Environment(trim_blocks=True, lstrip_blocks=False)916 tmpl = env.from_string(" {% if True +%}\n {% endif %}")917 assert tmpl.render() == " \n "918919 def test_no_trim_outer(self, env):920 env = Environment(trim_blocks=True, lstrip_blocks=False)921 tmpl = env.from_string("{% if True %}X{% endif +%}\nmore things")922 assert tmpl.render() == "X\nmore things"923924 def test_lstrip_no_trim(self, env):925 env = Environment(trim_blocks=True, lstrip_blocks=True)926 tmpl = env.from_string(" {% if True +%}\n {% endif %}")927 assert tmpl.render() == "\n"928929 def test_trim_blocks_false_with_no_trim(self, env):930 # Test that + is a NOP (but does not cause an error) if trim_blocks=False931 env = Environment(trim_blocks=False, lstrip_blocks=False)932 tmpl = env.from_string(" {% if True %}\n {% endif %}")933 assert tmpl.render() == " \n "934 tmpl = env.from_string(" {% if True +%}\n {% endif %}")935 assert tmpl.render() == " \n "936937 tmpl = env.from_string(" {# comment #}\n ")938 assert tmpl.render() == " \n "939 tmpl = env.from_string(" {# comment +#}\n ")940 assert tmpl.render() == " \n "941942 tmpl = env.from_string(" {% raw %}{% endraw %}\n ")943 assert tmpl.render() == " \n "944 tmpl = env.from_string(" {% raw %}{% endraw +%}\n ")945 assert tmpl.render() == " \n "946947 def test_trim_nested(self, env):948 env = Environment(trim_blocks=True, lstrip_blocks=True)949 tmpl = env.from_string(950 " {% if True %}\na {% if True %}\nb {% endif %}\nc {% endif %}"951 )952 assert tmpl.render() == "a b c "953954 def test_no_trim_nested(self, env):955 env = Environment(trim_blocks=True, lstrip_blocks=True)956 tmpl = env.from_string(957 " {% if True +%}\na {% if True +%}\nb {% endif +%}\nc {% endif %}"958 )959 assert tmpl.render() == "\na \nb \nc "960961 def test_comment_trim(self, env):962 env = Environment(trim_blocks=True, lstrip_blocks=True)963 tmpl = env.from_string(""" {# comment #}\n\n """)964 assert tmpl.render() == "\n "965966 def test_comment_no_trim(self, env):967 env = Environment(trim_blocks=True, lstrip_blocks=True)968 tmpl = env.from_string(""" {# comment +#}\n\n """)969 assert tmpl.render() == "\n\n "970971 def test_multiple_comment_trim_lstrip(self, env):972 env = Environment(trim_blocks=True, lstrip_blocks=True)973 tmpl = env.from_string(974 " {# comment #}\n\n{# comment2 #}\n \n{# comment3 #}\n\n "975 )976 assert tmpl.render() == "\n \n\n "977978 def test_multiple_comment_no_trim_lstrip(self, env):979 env = Environment(trim_blocks=True, lstrip_blocks=True)980 tmpl = env.from_string(981 " {# comment +#}\n\n{# comment2 +#}\n \n{# comment3 +#}\n\n "982 )983 assert tmpl.render() == "\n\n\n \n\n\n "984985 def test_raw_trim_lstrip(self, env):986 env = Environment(trim_blocks=True, lstrip_blocks=True)987 tmpl = env.from_string("{{x}}{% raw %}\n\n {% endraw %}\n\n{{ y }}")988 assert tmpl.render(x=1, y=2) == "1\n\n\n2"989990 def test_raw_no_trim_lstrip(self, env):991 env = Environment(trim_blocks=False, lstrip_blocks=True)992 tmpl = env.from_string("{{x}}{% raw %}\n\n {% endraw +%}\n\n{{ y }}")993 assert tmpl.render(x=1, y=2) == "1\n\n\n\n2"994995 # raw blocks do not process inner text, so start tag cannot ignore trim996 with pytest.raises(TemplateSyntaxError):997 tmpl = env.from_string("{{x}}{% raw +%}\n\n {% endraw +%}\n\n{{ y }}")998999 def test_no_trim_angle_bracket(self, env):1000 env = Environment(1001 "<%", "%>", "${", "}", "<%#", "%>", lstrip_blocks=True, trim_blocks=True,1002 )1003 tmpl = env.from_string(" <% if True +%>\n\n <% endif %>")1004 assert tmpl.render() == "\n\n"10051006 tmpl = env.from_string(" <%# comment +%>\n\n ")1007 assert tmpl.render() == "\n\n "10081009 def test_no_trim_php_syntax(self, env):1010 env = Environment(1011 "<?",1012 "?>",1013 "<?=",1014 "?>",1015 "<!--",1016 "-->",1017 lstrip_blocks=False,1018 trim_blocks=True,1019 )1020 tmpl = env.from_string(" <? if True +?>\n\n <? endif ?>")1021 assert tmpl.render() == " \n\n "1022 tmpl = env.from_string(" <!-- comment +-->\n\n ")1023 assert tmpl.render() == " \n\n "
modified:CHANGES.rst+1 added-1 removed
11 unmodified lines
12- Use :pep:`451` API to load templates with13 :class:`~loaders.PackageLoader`. :issue:`1168`14- Fix a bug that caused imported macros to not have access to the15 current template's globals. :issue:`688`16- Add ability to ignore ``trim_blocks`` using ``+%}``. :issue:`1036`171718Version 2.11.219--------------2021Released 2020-04-13
768 unmodified lines