diff --git a/lib/liquid/partial_cache.rb b/lib/liquid/partial_cache.rb index 56e10805..1ef52788 100644 --- a/lib/liquid/partial_cache.rb +++ b/lib/liquid/partial_cache.rb @@ -16,6 +16,8 @@ module Liquid template = template_factory.for(template_name) partial = template.parse(source, parse_context) + partial.name ||= template_name + cached_partials[template_name] = partial ensure parse_context.partial = false diff --git a/lib/liquid/tags/include.rb b/lib/liquid/tags/include.rb index 5b3ad8e0..7b9685ee 100644 --- a/lib/liquid/tags/include.rb +++ b/lib/liquid/tags/include.rb @@ -70,9 +70,11 @@ module Liquid old_template_name = context.template_name old_partial = context.partial + begin - context.template_name = template_name - context.partial = true + context.template_name = partial.name + context.partial = true + context.stack do @attributes.each do |key, value| context[key] = context.evaluate(value) diff --git a/lib/liquid/tags/render.rb b/lib/liquid/tags/render.rb index 0dc7215d..b9ae58ea 100644 --- a/lib/liquid/tags/render.rb +++ b/lib/liquid/tags/render.rb @@ -76,7 +76,7 @@ module Liquid render_partial_func = ->(var, forloop) { inner_context = context.new_isolated_subcontext - inner_context.template_name = template_name + inner_context.template_name = partial.name inner_context.partial = true inner_context['forloop'] = forloop if forloop diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 39bde833..e20ec1e0 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -15,7 +15,7 @@ module Liquid # template.render('user_name' => 'bob') # class Template - attr_accessor :root + attr_accessor :root, :name attr_reader :resource_limits, :warnings class TagRegistry diff --git a/test/integration/tags/include_tag_test.rb b/test/integration/tags/include_tag_test.rb index 442ab395..b86d40ee 100644 --- a/test/integration/tags/include_tag_test.rb +++ b/test/integration/tags/include_tag_test.rb @@ -356,4 +356,25 @@ class IncludeTagTest < Minitest::Test partials: { 'break' => "{% break %}" }, ) end + + def test_render_tag_renders_error_with_template_name + assert_template_result( + 'Liquid error (foo line 1): standard error', + "{% include 'foo' with errors %}", + { 'errors' => ErrorDrop.new }, + partials: { 'foo' => '{{ foo.standard_error }}' }, + render_errors: true, + ) + end + + def test_render_tag_renders_error_with_template_name_from_template_factory + assert_template_result( + 'Liquid error (some/path/foo line 1): standard error', + "{% include 'foo' with errors %}", + { 'errors' => ErrorDrop.new }, + partials: { 'foo' => '{{ foo.standard_error }}' }, + template_factory: StubTemplateFactory.new, + render_errors: true, + ) + end end # IncludeTagTest diff --git a/test/integration/tags/render_tag_test.rb b/test/integration/tags/render_tag_test.rb index f9ffc2f3..01485cfa 100644 --- a/test/integration/tags/render_tag_test.rb +++ b/test/integration/tags/render_tag_test.rb @@ -264,4 +264,25 @@ class RenderTagTest < Minitest::Test }, ) end + + def test_render_tag_renders_error_with_template_name + assert_template_result( + 'Liquid error (foo line 1): standard error', + "{% render 'foo' with errors %}", + { 'errors' => ErrorDrop.new }, + partials: { 'foo' => '{{ foo.standard_error }}' }, + render_errors: true, + ) + end + + def test_render_tag_renders_error_with_template_name_from_template_factory + assert_template_result( + 'Liquid error (some/path/foo line 1): standard error', + "{% render 'foo' with errors %}", + { 'errors' => ErrorDrop.new }, + partials: { 'foo' => '{{ foo.standard_error }}' }, + template_factory: StubTemplateFactory.new, + render_errors: true, + ) + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index c1514bb3..34d85aeb 100755 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -39,11 +39,12 @@ module Minitest def assert_template_result( expected, template, assigns = {}, - message: nil, partials: nil, error_mode: nil, render_errors: false + message: nil, partials: nil, error_mode: nil, render_errors: false, + template_factory: nil ) template = Liquid::Template.parse(template, line_numbers: true, error_mode: error_mode&.to_sym) file_system = StubFileSystem.new(partials || {}) - registers = Liquid::Registers.new(file_system: file_system) + registers = Liquid::Registers.new(file_system: file_system, template_factory: template_factory) context = Liquid::Context.build(static_environments: assigns, rethrow_errors: !render_errors, registers: registers) output = template.render(context) assert_equal(expected, output, message) @@ -209,8 +210,10 @@ class StubTemplateFactory @count = 0 end - def for(_template_name) + def for(template_name) @count += 1 - Liquid::Template.new + template = Liquid::Template.new + template.name = "some/path/" + template_name + template end end diff --git a/test/unit/partial_cache_unit_test.rb b/test/unit/partial_cache_unit_test.rb index 6bf8c5cd..6a20efbb 100644 --- a/test/unit/partial_cache_unit_test.rb +++ b/test/unit/partial_cache_unit_test.rb @@ -156,4 +156,22 @@ class PartialCacheUnitTest < Minitest::Test assert_equal(1, shared_file_system.file_read_count) end + + def test_uses_template_name_from_template_factory + template_factory = StubTemplateFactory.new + context = Liquid::Context.build( + registers: { + file_system: StubFileSystem.new('my_partial' => 'my partial body'), + template_factory: template_factory, + }, + ) + + partial = Liquid::PartialCache.load( + 'my_partial', + context: context, + parse_context: Liquid::ParseContext.new, + ) + + assert_equal('some/path/my_partial', partial.name) + end end