mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
working with default parsing for args
This commit is contained in:
@@ -36,8 +36,14 @@
|
||||
|
||||
{% # Snippet input %}
|
||||
{% snippet "input" %}
|
||||
<label>User:</label>
|
||||
<input />
|
||||
<div>
|
||||
<label>{{ label }}</label>
|
||||
<input type={{ type }}>
|
||||
</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% snippet "league" %}
|
||||
<h1>Welcome to the {{ team }} of super {{ name }}</h1>
|
||||
{% endsnippet %}
|
||||
|
||||
|
||||
@@ -48,8 +54,9 @@
|
||||
</marquee>
|
||||
{% endsnippet %}
|
||||
|
||||
{% render 'input' %}
|
||||
{% render 'input' %}
|
||||
{% render 'league', team: "league", name: "Evil" %}
|
||||
{% render 'input', label: "User", type: "text" %}
|
||||
{% render 'input', label: "Pass", type: "password" %}
|
||||
{% render 'banner' %}
|
||||
|
||||
{% endsnippet %}
|
||||
|
||||
@@ -68,7 +68,13 @@ module Liquid
|
||||
|
||||
# Inline snippets take precedence over external snippets
|
||||
if (inline_snippet = context.registers[:inline_snippet][template_name])
|
||||
return output << inline_snippet.render(context)
|
||||
inner_context = context.new_isolated_subcontext
|
||||
|
||||
@attributes.each do |key, value|
|
||||
inner_context[key] = context.evaluate(value)
|
||||
end
|
||||
|
||||
return output << inline_snippet.render(inner_context)
|
||||
end
|
||||
|
||||
partial = PartialCache.load(
|
||||
|
||||
@@ -14,12 +14,17 @@ module Liquid
|
||||
# value
|
||||
# {% endsnippet %}
|
||||
class Snippet < Block
|
||||
# SYNTAX = /(#{QuotedString}+) +\|(#{VariableSegment}*)\|/o
|
||||
SYNTAX = /(#{QuotedString}+)/o
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
if markup =~ SYNTAX
|
||||
# binding.irb
|
||||
@to = Regexp.last_match(1)
|
||||
# arg = Regexp.last_match(2)
|
||||
|
||||
# @args = []
|
||||
# @args << arg if arg
|
||||
else
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
|
||||
end
|
||||
@@ -38,7 +43,8 @@ module Liquid
|
||||
end
|
||||
|
||||
def snippet_body
|
||||
@body
|
||||
body = @body
|
||||
body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -71,4 +71,37 @@ class SnippetTest < Minitest::Test
|
||||
|
||||
assert_template_result(expected, template)
|
||||
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, value| %}
|
||||
<input type="{{ type }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
{%- render "input", type: "text" -%}
|
||||
LIQUID
|
||||
expected = <<~OUTPUT
|
||||
|
||||
<input type="text" />
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template)
|
||||
end
|
||||
|
||||
def test_render_inline_snippet_with_multiple_arguments
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet "input" |type, value| %}
|
||||
<input type="{{ type }}" value="{{ value }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
{%- render "input", type: "text", value: "Hello" -%}
|
||||
LIQUID
|
||||
expected = <<~OUTPUT
|
||||
|
||||
<input type="text" value="Hello" />
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user