diff --git a/lib/liquid/locales/en.yml b/lib/liquid/locales/en.yml index f33c61ce..e56f089a 100644 --- a/lib/liquid/locales/en.yml +++ b/lib/liquid/locales/en.yml @@ -4,6 +4,7 @@ tag_unexpected_args: "Syntax Error in '%{tag}' - Valid syntax: %{tag}" assign: "Syntax Error in 'assign' - Valid syntax: assign [var] = [source]" capture: "Syntax Error in 'capture' - Valid syntax: capture [var]" + snippet: "Syntax Error in 'snippet' - Valid syntax: snippet [quoted string]" case: "Syntax Error in 'case' - Valid syntax: case [condition]" case_invalid_when: "Syntax Error in tag 'case' - Valid when condition: {% when [condition] [or condition2...] %}" case_invalid_else: "Syntax Error in tag 'case' - Valid else condition: {% else %} (no parameters) " diff --git a/lib/liquid/partial_cache.rb b/lib/liquid/partial_cache.rb index f49d14d9..2d28eddf 100644 --- a/lib/liquid/partial_cache.rb +++ b/lib/liquid/partial_cache.rb @@ -9,7 +9,7 @@ module Liquid 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.rb b/lib/liquid/tags.rb index 916a63bd..a7e7024f 100644 --- a/lib/liquid/tags.rb +++ b/lib/liquid/tags.rb @@ -19,6 +19,7 @@ require_relative "tags/comment" require_relative "tags/raw" require_relative "tags/render" require_relative "tags/cycle" +require_relative "tags/snippet" module Liquid module Tags @@ -42,6 +43,7 @@ module Liquid 'if' => If, 'echo' => Echo, 'tablerow' => TableRow, + 'snippet' => Snippet, }.freeze end end diff --git a/lib/liquid/tags/render.rb b/lib/liquid/tags/render.rb index 3615b1b3..c9830e16 100644 --- a/lib/liquid/tags/render.rb +++ b/lib/liquid/tags/render.rb @@ -66,6 +66,14 @@ module Liquid template_name = @template_name_expr raise ::ArgumentError unless template_name.is_a?(String) + puts '---------------' + puts template_name + puts '---------------' + if (inline_snippet = context.registers[:inline_snippet][template_name]) + puts '!!!' + p(inline_snippet) + end + partial = PartialCache.load( template_name, context: context, diff --git a/lib/liquid/tags/snippet.rb b/lib/liquid/tags/snippet.rb new file mode 100644 index 00000000..6f36dc88 --- /dev/null +++ b/lib/liquid/tags/snippet.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module Liquid + # @liquid_public_docs + # @liquid_type tag + # @liquid_category theme + # @liquid_name snippet + # @liquid_summary + # Creates a new variable with a string value. + # @liquid_description + # You can create complex strings with Liquid logic and variables. + # @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 + + def initialize(tag_name, markup, options) + super + if markup =~ SYNTAX + @to = Regexp.last_match(1) + else + raise SyntaxError, options[:locale].t("errors.syntax.snippet") + end + end + + def render(context) + context.registers[:inline_snippet] ||= {} + context.registers[@to] = @body + '' + end + end +end diff --git a/test/integration/tags/snippet_test.rb b/test/integration/tags/snippet_test.rb new file mode 100644 index 00000000..e33823f7 --- /dev/null +++ b/test/integration/tags/snippet_test.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require 'test_helper' + +class SnippetTest < Minitest::Test + include Liquid + + def xtest_valid_inline_snippet + template = <<~LIQUID.strip + {% snippet "input" %} + Hey + {% endsnippet %} + LIQUID + expected = '' + + assert_template_result(expected, template) + end + + def xtest_invalid_inline_snippet + template = <<~LIQUID.strip + {% snippet input %} + Hey + {% endsnippet %} + LIQUID + expected = "Syntax Error in 'snippet' - Valid syntax: snippet [quoted string]" + + assert_match_syntax_error(expected, template) + end + + def test_render_inline_snippet + template = <<~LIQUID.strip + {% snippet "input" %} + Hey + {% endsnippet %} + + {%- render "input" %}{% render "input" %} + LIQUID + expected = <<~OUTPUT.strip + HeyHey + OUTPUT + + assert_template_result(expected, template, partials: { + 'input' => 'Hey', + }) + end +end