From 63583ffe5b0e5c38dc719c2ab8cfd99fd9e7d6ba Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Wed, 11 Dec 2024 10:16:58 -0500 Subject: [PATCH] Write one value at a time for array variables (#1863) * Write one value at a time for array variables * Handle recursive array --- lib/liquid/variable.rb | 18 ++++++++++++------ test/integration/variable_test.rb | 4 ++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 372ee4dbf..2bcf871ea 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -95,15 +95,21 @@ def render(context) def render_to_output_buffer(context, output) obj = render(context) + render_obj_to_output(obj, output) + output + end - if obj.is_a?(Array) - output << obj.join - elsif obj.nil? - else + def render_obj_to_output(obj, output) + case obj + when NilClass + # Do nothing + when Array + obj.each do |o| + render_obj_to_output(o, output) + end + when output << obj.to_s end - - output end def disabled?(_context) diff --git a/test/integration/variable_test.rb b/test/integration/variable_test.rb index 22f2b64e8..19922c191 100644 --- a/test/integration/variable_test.rb +++ b/test/integration/variable_test.rb @@ -130,6 +130,10 @@ def test_render_symbol assert_template_result('bar', '{{ foo }}', { 'foo' => :bar }) end + def test_nested_array + assert_template_result('', '{{ foo }}', { 'foo' => [[nil]] }) + end + def test_dynamic_find_var assert_template_result('bar', '{{ [key] }}', { 'key' => 'foo', 'foo' => 'bar' }) end