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