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
Provenance
- Commit
6b01227- Tree
2046b95- Author
Amy <leiamy12@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
9b718ed
4 files changed+136 added−5 removed
modified:docs/templates.rst+9 added
228 unmodified lines229 <div>230 {%+ if something %}yay{% endif %}231 </div>232 233+Similarly, you can manually disable the ``trim_blocks`` behavior by234+putting a plus sign (``+``) at the end of a block::235+236+ <div>237+ {% if something +%}238+ yay239+ {% endif %}240+ </div>241+242 You can also strip whitespace in templates by hand. If you add a minus243 sign (``-``) to the start or end of a block (e.g. a :ref:`for-loop` tag), a244 comment, or a variable expression, the whitespaces before or after245 that block will be removed::1583 unmodified linesmodified:src/jinja2/lexer.py+8 added-4 removed
507 unmodified lines508 # 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 line519 ],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 lines542 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 linesmodified:tests/test_lexnparse.py+118 added
901 unmodified lines902 ${item}903 <!--- endfor -->"""904 )905 assert tmpl.render(seq=range(5)) == "01234"906+907+908+class 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() == " "913+914+ 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 "918+919+ 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"923+924+ 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"928+929+ 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 "936+937+ tmpl = env.from_string(" {# comment #}\n ")938+ assert tmpl.render() == " \n "939+ tmpl = env.from_string(" {# comment +#}\n ")940+ assert tmpl.render() == " \n "941+942+ 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 "946+947+ 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 "953+954+ 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 "960+961+ 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 "965+966+ 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 "970+971+ 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 "977+978+ 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 "984+985+ 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"989+990+ 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"994+995+ # 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 }}")998+999+ 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"1005+1006+ tmpl = env.from_string(" <%# comment +%>\n\n ")1007+ assert tmpl.render() == "\n\n "1008+1009+ 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 lines12 - 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`17 17-18 Version 2.11.219 --------------20 21 Released 2020-04-13768 unmodified lines