From a9defe5468d602dac0334b8a2dae061bc119999a Mon Sep 17 00:00:00 2001 From: nagibatorz Date: Mon, 9 Nov 2026 18:54:41 -0800 Subject: [PATCH 1/2] test: trailing whitespace control must not strip partial leading whitespace A template ending in -%} or -}} leaves parse_context.trim_whitespace set, and PartialCache.load parses the partial with the parent template's parse context, so the partial's leading whitespace is stripped. Repro for #2140. --- test/integration/tags/render_tag_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/integration/tags/render_tag_test.rb b/test/integration/tags/render_tag_test.rb index 6773ad251..cdebfb9cc 100644 --- a/test/integration/tags/render_tag_test.rb +++ b/test/integration/tags/render_tag_test.rb @@ -165,6 +165,15 @@ def test_render_tag_within_if_statement ) end + def test_trailing_whitespace_control_does_not_strip_partial_leading_whitespace + options = { partials: { 'p' => ' [p]' } } + assert_template_result('a [p]', "a {% render 'p' %}", **options) + assert_template_result('a [p]', "a {% render 'p' -%}", **options) + assert_template_result(' [p]| [p]', "{% render 'p' %}|{% render 'p' -%}", **options) + assert_template_result(' [p]', "{% render 'p' %}{% assign x = 1 -%}", **options) + assert_template_result(' [p]1', "{% render 'p' %}{{ 1 -}}", **options) + end + def test_break_through_render options = { partials: { 'break' => '{% break %}' } } assert_template_result('1', '{% for i in (1..3) %}{{ i }}{% break %}{{ i }}{% endfor %}', **options) From a1d4a8c1dedc9d7b1d3ee7a1ff05bb8e0028a580 Mon Sep 17 00:00:00 2001 From: nagibatorz Date: Mon, 9 Nov 2026 18:54:49 -0800 Subject: [PATCH 2/2] fix: parse partials with whitespace trimming disabled A template ending in -%} or -}} leaves trim_whitespace set on the shared parse context, and PartialCache.load parsed partials with that context, stripping the partial's leading whitespace. Clear the flag while parsing the partial and restore it afterwards. Fixes #2140. --- lib/liquid/partial_cache.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/liquid/partial_cache.rb b/lib/liquid/partial_cache.rb index f49d14d90..c46a5cf13 100644 --- a/lib/liquid/partial_cache.rb +++ b/lib/liquid/partial_cache.rb @@ -12,6 +12,8 @@ def self.load(template_name, context:, parse_context:) source = file_system.read_template_file(template_name) parse_context.partial = true + trim_whitespace = parse_context.trim_whitespace + parse_context.trim_whitespace = false template_factory = context.registers[:template_factory] template = template_factory.for(template_name) @@ -28,6 +30,7 @@ def self.load(template_name, context:, parse_context:) cached_partials[cache_key] = partial ensure parse_context.partial = false + parse_context.trim_whitespace = trim_whitespace end end end