Skip to content

Commit

Permalink
set context's template_name with template's name
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmichaelgo committed Feb 28, 2023
1 parent 428c66f commit 24dceef
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 57 deletions.
7 changes: 1 addition & 6 deletions lib/liquid/partial_cache.rb
Expand Up @@ -16,12 +16,7 @@ def self.load(template_name, context:, parse_context:)
template = template_factory.for(template_name)

partial = template.parse(source, parse_context)

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
partial.name ||= template_name

cached_partials[template_name] = partial
ensure
Expand Down
4 changes: 2 additions & 2 deletions lib/liquid/tags/include.rb
Expand Up @@ -72,9 +72,9 @@ def render_to_output_buffer(context, output)
old_partial = context.partial

begin
context.template_name = partial.name if partial.name

context.template_name = partial.name
context.partial = true

context.stack do
@attributes.each do |key, value|
context[key] = context.evaluate(value)
Expand Down
4 changes: 1 addition & 3 deletions lib/liquid/tags/render.rb
Expand Up @@ -76,9 +76,7 @@ def render_tag(context, output)

render_partial_func = ->(var, forloop) {
inner_context = context.new_isolated_subcontext

inner_context.template_name = partial.name if partial.name

inner_context.template_name = partial.name
inner_context.partial = true
inner_context['forloop'] = forloop if forloop

Expand Down
26 changes: 17 additions & 9 deletions test/integration/tags/include_tag_test.rb
Expand Up @@ -357,16 +357,24 @@ def test_break_through_include
)
end

def test_include_tag_renders_actual_template_name_for_error
original_file_system = Liquid::Template.file_system

Liquid::Template.file_system = MemoryFileSystem.new(
'/some/path/snippets/foo.liquid' => "{{ foo.standard_error }}",
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

template = Liquid::Template.parse("{% include 'foo' with errors %}", line_numbers: true)
assert_equal('Liquid error (/some/path/snippets/foo line 1): standard error', template.render('errors' => ErrorDrop.new))
ensure
Liquid::Template.file_system = original_file_system
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
28 changes: 17 additions & 11 deletions test/integration/tags/render_tag_test.rb
Expand Up @@ -265,18 +265,24 @@ def test_render_tag_with_drop
)
end

def test_render_tag_renders_actual_template_name_for_error
original_file_system = Liquid::Template.file_system

context = Liquid::Context.new('errors' => ErrorDrop.new)

context.registers[:file_system] = MemoryFileSystem.new(
'/some/path/snippets/foo.liquid' => "{{ foo.standard_error }}",
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

template = Liquid::Template.parse("{% render 'foo' with errors %}", line_numbers: true)
assert_equal('Liquid error (/some/path/snippets/foo line 1): standard error', template.render(context))
ensure
Liquid::Template.file_system = original_file_system
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
33 changes: 7 additions & 26 deletions test/test_helper.rb
Expand Up @@ -39,11 +39,12 @@ module Assertions

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)
Expand Down Expand Up @@ -209,30 +210,10 @@ def initialize
@count = 0
end

def for(_template_name)
def for(template_name)
@count += 1
Liquid::Template.new
end
end

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")
template = Liquid::Template.new
template.name = "some/path/" + template_name
template
end
end
18 changes: 18 additions & 0 deletions test/unit/partial_cache_unit_test.rb
Expand Up @@ -156,4 +156,22 @@ def test_cache_state_is_shared_for_subcontexts

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

0 comments on commit 24dceef

Please sign in to comment.