mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-17 01:40:42 -07:00
snippet using inner context
This commit is contained in:
@@ -35,29 +35,20 @@
|
||||
{% snippet "main" %}
|
||||
|
||||
{% # Snippet input %}
|
||||
{% snippet "input" %}
|
||||
<div>
|
||||
<label>{{ label }}</label>
|
||||
<input type={{ type }}>
|
||||
{% snippet "input" |type, name| %}
|
||||
<div>
|
||||
<label>{{ type | capitalize }}</label>
|
||||
<input type={{ type }}>
|
||||
</div>
|
||||
{% endsnippet %}
|
||||
|
||||
{% snippet "league" %}
|
||||
<h1>Welcome to the {{ team }} of super {{ name }}</h1>
|
||||
<h1>Welcome to the league of super evil</h1>
|
||||
{% endsnippet %}
|
||||
|
||||
|
||||
{% # Snippet banner %}
|
||||
{% snippet "banner" %}
|
||||
<marquee direction="up" height="100px">
|
||||
Welcome to my store!
|
||||
</marquee>
|
||||
{% 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' %}
|
||||
|
||||
@@ -69,12 +69,18 @@ module Liquid
|
||||
# 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(
|
||||
|
||||
@@ -14,17 +14,15 @@ module Liquid
|
||||
# value
|
||||
# {% endsnippet %}
|
||||
class Snippet < Block
|
||||
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 +30,10 @@ module Liquid
|
||||
|
||||
def render(context)
|
||||
context.registers[:inline_snippet] ||= {}
|
||||
context.registers[:inline_snippet][snippet_id] = snippet_body
|
||||
context.registers[:inline_snippet][snippet_id] = {
|
||||
body: snippet_body,
|
||||
args: @args,
|
||||
}
|
||||
''
|
||||
end
|
||||
|
||||
|
||||
@@ -73,7 +73,6 @@ class SnippetTest < Minitest::Test
|
||||
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| %}
|
||||
<input type="{{ type }}" />
|
||||
@@ -89,38 +88,78 @@ class SnippetTest < Minitest::Test
|
||||
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 %}
|
||||
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
|
||||
{%- render "input", type: "text", value: "Hello" -%}
|
||||
LIQUID
|
||||
expected = <<~OUTPUT
|
||||
|
||||
# <input type="text" value="Hello" />
|
||||
# OUTPUT
|
||||
<input type="text" value="Hello" />
|
||||
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| %}
|
||||
# <input type="{{ type }}" value="{{ value }}" />
|
||||
# {% endsnippet %}
|
||||
def test_render_inline_snippet_empty_string_when_missing_argument
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet "input" |type| %}
|
||||
<input type="{{ type }}" value="{{ value }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
# {%- render "input", type: "text", value: "Hello" -%}
|
||||
{%- render "input", type: "text" -%}
|
||||
LIQUID
|
||||
expected = <<~OUTPUT
|
||||
|
||||
# {{ type }}
|
||||
# {{ value }}
|
||||
# LIQUID
|
||||
# expected = <<~OUTPUT
|
||||
<input type="text" value="" />
|
||||
OUTPUT
|
||||
|
||||
# <input type="text" value="Hello" />
|
||||
# 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| %}
|
||||
<input type="{{ type }}" value="{{ value }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
{%- render "input", type: "text", value: "Hello" -%}
|
||||
|
||||
{{ type }}
|
||||
{{ value }}
|
||||
LIQUID
|
||||
expected = <<~OUTPUT
|
||||
|
||||
<input type="text" value="Hello" />
|
||||
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template)
|
||||
end
|
||||
|
||||
def test_render_multiple_inline_snippets_without_leaking_context
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet "input" |type| %}
|
||||
<input type="{{ type }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
{% snippet "banner"%}
|
||||
{{ type }}
|
||||
{% endsnippet %}
|
||||
|
||||
{%- render "input", type: "text" -%}
|
||||
{%- render "banner" -%}
|
||||
LIQUID
|
||||
expected = <<~OUTPUT.strip
|
||||
|
||||
<input type="text" />
|
||||
|
||||
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user