set context's template_name with template's name

This commit is contained in:
Michael Go
2023-02-28 10:55:38 -04:00
parent 428c66ffac
commit 24dceef552
7 changed files with 63 additions and 57 deletions
+1 -6
View File
@@ -16,12 +16,7 @@ module Liquid
template = template_factory.for(template_name) template = template_factory.for(template_name)
partial = template.parse(source, parse_context) partial = template.parse(source, parse_context)
partial.name ||= template_name
partial.name ||= if context.registers[:file_system]&.respond_to?(:actual_template_name)
context.registers[:file_system].actual_template_name(template_name)
else
template_name
end
cached_partials[template_name] = partial cached_partials[template_name] = partial
ensure ensure
+2 -2
View File
@@ -72,9 +72,9 @@ module Liquid
old_partial = context.partial old_partial = context.partial
begin begin
context.template_name = partial.name if partial.name context.template_name = partial.name
context.partial = true context.partial = true
context.stack do context.stack do
@attributes.each do |key, value| @attributes.each do |key, value|
context[key] = context.evaluate(value) context[key] = context.evaluate(value)
+1 -3
View File
@@ -76,9 +76,7 @@ module Liquid
render_partial_func = ->(var, forloop) { render_partial_func = ->(var, forloop) {
inner_context = context.new_isolated_subcontext inner_context = context.new_isolated_subcontext
inner_context.template_name = partial.name
inner_context.template_name = partial.name if partial.name
inner_context.partial = true inner_context.partial = true
inner_context['forloop'] = forloop if forloop inner_context['forloop'] = forloop if forloop
+17 -9
View File
@@ -357,16 +357,24 @@ class IncludeTagTest < Minitest::Test
) )
end end
def test_include_tag_renders_actual_template_name_for_error def test_render_tag_renders_error_with_template_name
original_file_system = Liquid::Template.file_system assert_template_result(
'Liquid error (foo line 1): standard error',
Liquid::Template.file_system = MemoryFileSystem.new( "{% include 'foo' with errors %}",
'/some/path/snippets/foo.liquid' => "{{ foo.standard_error }}", { 'errors' => ErrorDrop.new },
partials: { 'foo' => '{{ foo.standard_error }}' },
render_errors: true,
) )
end
template = Liquid::Template.parse("{% include 'foo' with errors %}", line_numbers: true) def test_render_tag_renders_error_with_template_name_from_template_factory
assert_equal('Liquid error (/some/path/snippets/foo line 1): standard error', template.render('errors' => ErrorDrop.new)) assert_template_result(
ensure 'Liquid error (some/path/foo line 1): standard error',
Liquid::Template.file_system = original_file_system "{% include 'foo' with errors %}",
{ 'errors' => ErrorDrop.new },
partials: { 'foo' => '{{ foo.standard_error }}' },
template_factory: StubTemplateFactory.new,
render_errors: true,
)
end end
end # IncludeTagTest end # IncludeTagTest
+17 -11
View File
@@ -265,18 +265,24 @@ class RenderTagTest < Minitest::Test
) )
end end
def test_render_tag_renders_actual_template_name_for_error def test_render_tag_renders_error_with_template_name
original_file_system = Liquid::Template.file_system assert_template_result(
'Liquid error (foo line 1): standard error',
context = Liquid::Context.new('errors' => ErrorDrop.new) "{% render 'foo' with errors %}",
{ 'errors' => ErrorDrop.new },
context.registers[:file_system] = MemoryFileSystem.new( partials: { 'foo' => '{{ foo.standard_error }}' },
'/some/path/snippets/foo.liquid' => "{{ foo.standard_error }}", render_errors: true,
) )
end
template = Liquid::Template.parse("{% render 'foo' with errors %}", line_numbers: true) def test_render_tag_renders_error_with_template_name_from_template_factory
assert_equal('Liquid error (/some/path/snippets/foo line 1): standard error', template.render(context)) assert_template_result(
ensure 'Liquid error (some/path/foo line 1): standard error',
Liquid::Template.file_system = original_file_system "{% render 'foo' with errors %}",
{ 'errors' => ErrorDrop.new },
partials: { 'foo' => '{{ foo.standard_error }}' },
template_factory: StubTemplateFactory.new,
render_errors: true,
)
end end
end end
+7 -26
View File
@@ -39,11 +39,12 @@ module Minitest
def assert_template_result( def assert_template_result(
expected, template, assigns = {}, 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) template = Liquid::Template.parse(template, line_numbers: true, error_mode: error_mode&.to_sym)
file_system = StubFileSystem.new(partials || {}) 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) context = Liquid::Context.build(static_environments: assigns, rethrow_errors: !render_errors, registers: registers)
output = template.render(context) output = template.render(context)
assert_equal(expected, output, message) assert_equal(expected, output, message)
@@ -209,30 +210,10 @@ class StubTemplateFactory
@count = 0 @count = 0
end end
def for(_template_name) def for(template_name)
@count += 1 @count += 1
Liquid::Template.new template = Liquid::Template.new
end template.name = "some/path/" + template_name
end template
class MemoryFileSystem
def initialize(values)
# values is a hash of template_path => template_source
@snippets = {}
values.each do |file_path, source|
key = file_path.split('/').last.delete_suffix(".liquid")
@snippets[key] = {
source: source,
actual_template_name: file_path,
}
end
end
def read_template_file(template_name)
@snippets[template_name][:source]
end
def actual_template_name(template_name)
@snippets[template_name][:actual_template_name].delete_suffix(".liquid")
end end
end end
+18
View File
@@ -156,4 +156,22 @@ class PartialCacheUnitTest < Minitest::Test
assert_equal(1, shared_file_system.file_read_count) assert_equal(1, shared_file_system.file_read_count)
end 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 end