diff --git a/example/server/templates/index.liquid b/example/server/templates/index.liquid
index 1197b103f..529055089 100644
--- a/example/server/templates/index.liquid
+++ b/example/server/templates/index.liquid
@@ -35,29 +35,20 @@
{% snippet "main" %}
{% # Snippet input %}
- {% snippet "input" %}
-
-
-
+ {% snippet "input" |type, name| %}
+
+
+
{% endsnippet %}
{% snippet "league" %}
-
Welcome to the {{ team }} of super {{ name }}
+
Welcome to the league of super evil
{% endsnippet %}
-
- {% # Snippet banner %}
- {% snippet "banner" %}
-
- {% endsnippet %}
-
- {% render 'league', team: "league", name: "Evil" %}
- {% render 'input', label: "User", type: "text" %}
- {% render 'input', label: "Pass", type: "password" %}
- {% render 'banner' %}
+ {% render "league" %}
+ {% render "input", type: "text" %}
+ {% render "input", type: "password" %}
{% endsnippet %}
{% render 'main' %}
diff --git a/lib/liquid/tags/render.rb b/lib/liquid/tags/render.rb
index 51211e177..14274c064 100644
--- a/lib/liquid/tags/render.rb
+++ b/lib/liquid/tags/render.rb
@@ -69,12 +69,18 @@ def render_tag(context, output)
# Inline snippets take precedence over external snippets
if (inline_snippet = context.registers[:inline_snippet][template_name])
inner_context = context.new_isolated_subcontext
+ # binding.irb
+ snippet_body = inline_snippet[:body]
+ snippet_args = inline_snippet[:args]
+ # Validate and set the arguments in the inner context
@attributes.each do |key, value|
- inner_context[key] = context.evaluate(value)
+ if snippet_args.include?(key)
+ inner_context[key] = context.evaluate(value)
+ end
end
- return output << inline_snippet.render(inner_context)
+ return output << snippet_body.render(inner_context)
end
partial = PartialCache.load(
diff --git a/lib/liquid/tags/snippet.rb b/lib/liquid/tags/snippet.rb
index fbf43f493..752266d54 100644
--- a/lib/liquid/tags/snippet.rb
+++ b/lib/liquid/tags/snippet.rb
@@ -14,17 +14,16 @@ module Liquid
# value
# {% endsnippet %}
class Snippet < Block
- SYNTAX = /(#{QuotedString}+) +\|(#{VariableSegment}*)\|/o
+ # SYNTAX = /(#{QuotedString}+) +\|(#{VariableSegment}*)\|/o
+ SYNTAX = /(#{QuotedString})(?:\s*\|\s*([\w\s,]+)\s*\|)?/o
def initialize(tag_name, markup, options)
super
if markup =~ SYNTAX
- # binding.irb
@to = Regexp.last_match(1)
- arg = Regexp.last_match(2)
+ args = Regexp.last_match(2)
- @args = []
- @args << arg if arg
+ @args = args ? args.split(/\s*,\s*/) : []
else
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
end
@@ -32,7 +31,11 @@ def initialize(tag_name, markup, options)
def render(context)
context.registers[:inline_snippet] ||= {}
- context.registers[:inline_snippet][snippet_id] = snippet_body
+ # context.registers[:inline_snippet][snippet_id] = snippet_body
+ context.registers[:inline_snippet][snippet_id] = {
+ body: snippet_body,
+ args: @args,
+ }
''
end
diff --git a/test/integration/tags/snippet_test.rb b/test/integration/tags/snippet_test.rb
index b717e450f..3648065b0 100644
--- a/test/integration/tags/snippet_test.rb
+++ b/test/integration/tags/snippet_test.rb
@@ -73,7 +73,6 @@ def test_render_multiple_inline_snippets
end
def test_render_inline_snippet_with_argument
- # This passes whether or not we have the new or old SYNTAX
template = <<~LIQUID.strip
{% snippet "input" |type| %}
@@ -89,38 +88,55 @@ def test_render_inline_snippet_with_argument
assert_template_result(expected, template)
end
- # def test_render_inline_snippet_with_multiple_arguments
- # template = <<~LIQUID.strip
- # {% snippet "input" |type, value| %}
- #
- # {% endsnippet %}
+ def test_render_inline_snippet_with_multiple_arguments
+ template = <<~LIQUID.strip
+ {% snippet "input" |type, value| %}
+
+ {% endsnippet %}
+
+ {%- render "input", type: "text", value: "Hello" -%}
+ LIQUID
+ expected = <<~OUTPUT
+
+
+ OUTPUT
+
+ assert_template_result(expected, template)
+ end
+
+ def test_render_inline_snippet_empty_string_when_missing_argument
+ template = <<~LIQUID.strip
+ {% snippet "input" |type| %}
+
+ {% endsnippet %}
- # {%- render "input", type: "text", value: "Hello" -%}
- # LIQUID
- # expected = <<~OUTPUT
+ {%- render "input", type: "text" -%}
+ LIQUID
+ expected = <<~OUTPUT
- #
- # OUTPUT
+
+ OUTPUT
+
+ assert_template_result(expected, template)
+ end
- # assert_template_result(expected, template)
- # end
+ def test_render_inline_snippet_shouldnt_leak_context
+ template = <<~LIQUID.strip
+ {% snippet "input" |type, value| %}
+
+ {% endsnippet %}
- # def test_render_inline_snippet_shouldnt_leak_context
- # template = <<~LIQUID.strip
- # {% snippet "input" |type, value| %}
- #
- # {% endsnippet %}
+ {%- render "input", type: "text", value: "Hello" -%}
- # {%- render "input", type: "text", value: "Hello" -%}
+ {{ type }}
+ {{ value }}
+ LIQUID
+ expected = <<~OUTPUT
- # {{ type }}
- # {{ value }}
- # LIQUID
- # expected = <<~OUTPUT
+
- #
- # OUTPUT
+ OUTPUT
- # assert_template_result(expected, template)
- # end
+ assert_template_result(expected, template)
+ end
end