From b397513f8bb038b22dbb1f96b7329becbded71d6 Mon Sep 17 00:00:00 2001 From: Guilherme Carreiro Date: Fri, 20 Sep 2024 11:24:12 +0200 Subject: [PATCH] Introduce inline snippets prototype --- lib/liquid/partial_cache.rb | 2 +- lib/liquid/tags/render.rb | 7 ++----- lib/liquid/tags/snippet.rb | 18 +++++++++++++----- test/integration/tags/snippet_test.rb | 17 ++++++++--------- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/lib/liquid/partial_cache.rb b/lib/liquid/partial_cache.rb index 2d28eddf4..f49d14d90 100644 --- a/lib/liquid/partial_cache.rb +++ b/lib/liquid/partial_cache.rb @@ -9,7 +9,7 @@ def self.load(template_name, context:, parse_context:) return cached if cached file_system = context.registers[:file_system] - source = file_system.read_template_file(template_name) + source = file_system.read_template_file(template_name) parse_context.partial = true diff --git a/lib/liquid/tags/render.rb b/lib/liquid/tags/render.rb index c9830e161..b40700c87 100644 --- a/lib/liquid/tags/render.rb +++ b/lib/liquid/tags/render.rb @@ -66,12 +66,9 @@ def render_tag(context, output) template_name = @template_name_expr raise ::ArgumentError unless template_name.is_a?(String) - puts '---------------' - puts template_name - puts '---------------' + # Inline snippets take precedence over external snippets if (inline_snippet = context.registers[:inline_snippet][template_name]) - puts '!!!' - p(inline_snippet) + return output << inline_snippet.render(context) end partial = PartialCache.load( diff --git a/lib/liquid/tags/snippet.rb b/lib/liquid/tags/snippet.rb index 6f36dc88b..8998e8203 100644 --- a/lib/liquid/tags/snippet.rb +++ b/lib/liquid/tags/snippet.rb @@ -6,15 +6,13 @@ module Liquid # @liquid_category theme # @liquid_name snippet # @liquid_summary - # Creates a new variable with a string value. + # Creates a new inline snippet using a string value as the identifier. # @liquid_description - # You can create complex strings with Liquid logic and variables. + # You can create inline snippets to make your Liquid code more modular. # @liquid_syntax # {% snippet "input" %} # value # {% endsnippet %} - # @liquid_syntax_keyword variable The name of the variable being created. - # @liquid_syntax_keyword value The value you want to assign to the variable. class Snippet < Block SYNTAX = /(#{QuotedString}+)/o @@ -29,8 +27,18 @@ def initialize(tag_name, markup, options) def render(context) context.registers[:inline_snippet] ||= {} - context.registers[@to] = @body + context.registers[:inline_snippet][snippet_id] = snippet_body '' end + + private + + def snippet_id + @to[1, @to.size - 2] + end + + def snippet_body + @body + end end end diff --git a/test/integration/tags/snippet_test.rb b/test/integration/tags/snippet_test.rb index e33823f76..7db4bafe5 100644 --- a/test/integration/tags/snippet_test.rb +++ b/test/integration/tags/snippet_test.rb @@ -5,7 +5,7 @@ class SnippetTest < Minitest::Test include Liquid - def xtest_valid_inline_snippet + def test_valid_inline_snippet template = <<~LIQUID.strip {% snippet "input" %} Hey @@ -16,7 +16,7 @@ def xtest_valid_inline_snippet assert_template_result(expected, template) end - def xtest_invalid_inline_snippet + def test_invalid_inline_snippet template = <<~LIQUID.strip {% snippet input %} Hey @@ -30,17 +30,16 @@ def xtest_invalid_inline_snippet def test_render_inline_snippet template = <<~LIQUID.strip {% snippet "input" %} - Hey + Hey {% endsnippet %} - {%- render "input" %}{% render "input" %} + {%- render "input" -%} LIQUID - expected = <<~OUTPUT.strip - HeyHey + expected = <<~OUTPUT + + Hey OUTPUT - assert_template_result(expected, template, partials: { - 'input' => 'Hey', - }) + assert_template_result(expected, template) end end