From efaec29fa6cb1b487ef8084ff70346e5be21fcd0 Mon Sep 17 00:00:00 2001 From: Mike Angell Date: Tue, 8 Oct 2019 06:37:16 +1100 Subject: [PATCH] Default to template name --- lib/liquid/tags/include.rb | 4 ++-- lib/liquid/tags/render.rb | 6 +++--- test/integration/tags/render_tag_test.rb | 26 ++++++++---------------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/lib/liquid/tags/include.rb b/lib/liquid/tags/include.rb index 3f4177f..1a7ba4a 100644 --- a/lib/liquid/tags/include.rb +++ b/lib/liquid/tags/include.rb @@ -29,7 +29,7 @@ module Liquid template_name = Regexp.last_match(1) variable_name = Regexp.last_match(3) - @alias_name = Regexp.last_match(5) || nil + @alias_name = Regexp.last_match(5) @variable_name_expr = variable_name ? Expression.parse(variable_name) : nil @template_name_expr = Expression.parse(template_name) @attributes = {} @@ -56,7 +56,7 @@ module Liquid parse_context: parse_context ) - context_variable_name = @alias_name ? @alias_name : template_name.split('/').last + context_variable_name = @alias_name || template_name.split('/').last variable = if @variable_name_expr context.evaluate(@variable_name_expr) diff --git a/lib/liquid/tags/render.rb b/lib/liquid/tags/render.rb index 1b12333..1e0bdcc 100644 --- a/lib/liquid/tags/render.rb +++ b/lib/liquid/tags/render.rb @@ -16,7 +16,7 @@ module Liquid template_name = Regexp.last_match(1) variable_name = Regexp.last_match(3) - @alias_name = Regexp.last_match(5) || nil + @alias_name = Regexp.last_match(5) @variable_name_expr = variable_name ? Expression.parse(variable_name) : nil @template_name_expr = Expression.parse(template_name) @@ -41,7 +41,7 @@ module Liquid parse_context: parse_context ) - context_variable_name = @alias_name ? @alias_name : "this" + context_variable_name = @alias_name || template_name.split('/').last variable = if @variable_name_expr context.evaluate(@variable_name_expr) @@ -57,7 +57,7 @@ module Liquid @attributes.each do |key, value| inner_context[key] = context.evaluate(value) end - inner_context[context_variable_name] = var + inner_context[context_variable_name] = var if @variable_name_expr partial.render_to_output_buffer(inner_context, output) end diff --git a/test/integration/tags/render_tag_test.rb b/test/integration/tags/render_tag_test.rb index 117ecb8..f2fbc55 100644 --- a/test/integration/tags/render_tag_test.rb +++ b/test/integration/tags/render_tag_test.rb @@ -166,17 +166,17 @@ class RenderTagTest < Minitest::Test assert_template_result('include usage is not allowed in this contextinclude usage is not allowed in this context', '{% render "nested_render_with_sibling_include" %}') end - def test_include_tag_with + def test_render_tag_with Liquid::Template.file_system = StubFileSystem.new( - 'product' => "Product: {{ this.title }} ", - 'product_alias' => "Product: {{ this.title }} ", + 'product' => "Product: {{ product.title }} ", + 'product_alias' => "Product: {{ product.title }} ", ) assert_template_result("Product: Draft 151cm ", "{% render 'product' with products[0] %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }]) end - def test_include_tag_with_alias + def test_render_tag_with_alias Liquid::Template.file_system = StubFileSystem.new( 'product' => "Product: {{ product.title }} ", 'product_alias' => "Product: {{ product.title }} ", @@ -186,7 +186,7 @@ class RenderTagTest < Minitest::Test "{% render 'product_alias' with products[0] as product %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }]) end - def test_include_tag_for_alias + def test_render_tag_for_alias Liquid::Template.file_system = StubFileSystem.new( 'product' => "Product: {{ product.title }} ", 'product_alias' => "Product: {{ product.title }} ", @@ -196,20 +196,10 @@ class RenderTagTest < Minitest::Test "{% render 'product_alias' for products as product %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }]) end - def test_include_tag_with_default_name + def test_render_tag_for Liquid::Template.file_system = StubFileSystem.new( - 'product' => "Product: {{ this.title }} ", - 'product_alias' => "Product: {{ this.title }} ", - ) - - assert_template_result("Product: Draft 151cm ", - "{% render 'product' %}", "product" => { 'title' => 'Draft 151cm' }) - end - - def test_include_tag_for - Liquid::Template.file_system = StubFileSystem.new( - 'product' => "Product: {{ this.title }} ", - 'product_alias' => "Product: {{ this.title }} ", + 'product' => "Product: {{ product.title }} ", + 'product_alias' => "Product: {{ product.title }} ", ) assert_template_result("Product: Draft 151cm Product: Element 155cm ",