mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Update inline snippets syntax
Previously, inline snippets syntax looked a bit
different, they:
- used strings as tag identifiers
- defined tag arguments {% snippet "input" |type| %}
This PR updates snippets to better reflect
the currently proposed syntax
Co-authored-by: Orlando Qiu <[email protected]>
This commit is contained in:
co-authored by
Orlando Qiu
parent
8c6149782d
commit
4d13f030f8
@@ -54,15 +54,15 @@ 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])
|
||||||
inner_context = context.new_isolated_subcontext
|
inner_context = context.new_isolated_subcontext
|
||||||
|
|
||||||
snippet_body = inline_snippet[:body]
|
snippet_body = inline_snippet[:body]
|
||||||
snippet_args = inline_snippet[:args]
|
|
||||||
# Validate and set the arguments in the inner context
|
|
||||||
@attributes.each do |key, value|
|
|
||||||
unless snippet_args.include?(key)
|
|
||||||
raise Liquid::ArgumentError, "Invalid argument `#{key}` for snippet `#{template_name}`"
|
|
||||||
end
|
|
||||||
|
|
||||||
|
context.scopes.each do |scope|
|
||||||
|
scope.each do |key, value|
|
||||||
|
inner_context[key] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@attributes.each do |key, value|
|
||||||
inner_context[key] = context.evaluate(value)
|
inner_context[key] = context.evaluate(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +1,25 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
# @liquid_public_docs
|
|
||||||
# @liquid_type tag
|
# @liquid_type tag
|
||||||
# @liquid_category theme
|
# @liquid_category theme
|
||||||
# @liquid_name snippet
|
# @liquid_name snippet
|
||||||
# @liquid_summary
|
# @liquid_summary
|
||||||
# Creates a new inline snippet using a string value as the identifier.
|
# Creates a new inline snippet.
|
||||||
# @liquid_description
|
# @liquid_description
|
||||||
# You can create inline snippets to make your Liquid code more modular.
|
# You can create inline snippets to make your Liquid code more modular.
|
||||||
# @liquid_syntax
|
# @liquid_syntax
|
||||||
# {% snippet "input" %}
|
# {% snippet input %}
|
||||||
# value
|
# value
|
||||||
# {% endsnippet %}
|
# {% endsnippet %}
|
||||||
|
|
||||||
class Snippet < Block
|
class Snippet < Block
|
||||||
SYNTAX = /(#{QuotedString})(?:\s*\|\s*([\w\s,]+)\s*\|)?/o
|
SYNTAX = /(#{VariableSignature}+)/o
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def initialize(tag_name, markup, options)
|
||||||
super
|
super
|
||||||
|
|
||||||
if markup =~ SYNTAX
|
if markup =~ SYNTAX
|
||||||
@to = Regexp.last_match(1)
|
@to = Regexp.last_match(1)
|
||||||
args = Regexp.last_match(2)
|
|
||||||
|
|
||||||
@args = args ? args.split(/\s*,\s*/) : []
|
|
||||||
else
|
else
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
|
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
|
||||||
end
|
end
|
||||||
@@ -30,22 +27,10 @@ module Liquid
|
|||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
context.registers[:inline_snippet] ||= {}
|
context.registers[:inline_snippet] ||= {}
|
||||||
context.registers[:inline_snippet][snippet_id] = {
|
context.registers[:inline_snippet][@to] = {
|
||||||
body: snippet_body,
|
body: @body,
|
||||||
args: @args,
|
|
||||||
}
|
}
|
||||||
''
|
''
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def snippet_id
|
|
||||||
@to[1, @to.size - 2]
|
|
||||||
end
|
|
||||||
|
|
||||||
def snippet_body
|
|
||||||
body = @body
|
|
||||||
body
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
def test_valid_inline_snippet
|
def test_valid_inline_snippet
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet "input" %}
|
{% snippet input %}
|
||||||
Hey
|
Hey
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
LIQUID
|
LIQUID
|
||||||
@@ -16,20 +16,9 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_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
|
def test_render_inline_snippet
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet "hey" %}
|
{% snippet hey %}
|
||||||
Hey
|
Hey
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
@@ -45,11 +34,11 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
def test_render_multiple_inline_snippets
|
def test_render_multiple_inline_snippets
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet "input" %}
|
{% snippet input %}
|
||||||
<input />
|
<input />
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
{% snippet "banner" %}
|
{% snippet banner %}
|
||||||
<marquee direction="up" height="100px">
|
<marquee direction="up" height="100px">
|
||||||
Welcome to my store!
|
Welcome to my store!
|
||||||
</marquee>
|
</marquee>
|
||||||
@@ -74,7 +63,7 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
def test_render_inline_snippet_with_argument
|
def test_render_inline_snippet_with_argument
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet "input" |type| %}
|
{% snippet input %}
|
||||||
<input type="{{ type }}" />
|
<input type="{{ type }}" />
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
@@ -88,9 +77,36 @@ class SnippetTest < Minitest::Test
|
|||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_inline_snippet_with_doc_tag
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% snippet input %}
|
||||||
|
{% doc %}
|
||||||
|
@param {string} type - Input type.
|
||||||
|
{% enddoc %}
|
||||||
|
|
||||||
|
<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
|
def test_render_inline_snippet_with_multiple_arguments
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet "input" |type, value| %}
|
{% snippet input %}
|
||||||
|
{% doc %}
|
||||||
|
@param {string} type - Input type.
|
||||||
|
@param {string} value - Input value.
|
||||||
|
{% enddoc %}
|
||||||
|
|
||||||
<input type="{{ type }}" value="{{ value }}" />
|
<input type="{{ type }}" value="{{ value }}" />
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
@@ -98,6 +114,8 @@ class SnippetTest < Minitest::Test
|
|||||||
LIQUID
|
LIQUID
|
||||||
expected = <<~OUTPUT
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<input type="text" value="Hello" />
|
<input type="text" value="Hello" />
|
||||||
OUTPUT
|
OUTPUT
|
||||||
|
|
||||||
@@ -106,24 +124,25 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
def test_render_inline_snippets_using_same_argument_name
|
def test_render_inline_snippets_using_same_argument_name
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet "input" |type| %}
|
{% snippet input %}
|
||||||
<input type="{{ type }}" />
|
<input type="{{ type }}" />
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
{% snippet "inputs" |type, value| %}
|
{% snippet inputs %}
|
||||||
<input type="{{ type }}" value="{{ value }}" />
|
<input type="{{ type }}" value="{{ value }}" />
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
{%- render "input", type: "text" -%}
|
{%- render "input", type: "text" -%}
|
||||||
{%- render "inputs", type: "password", value: "pass" -%}
|
{%- render "inputs", type: "password", value: "pass" -%}
|
||||||
LIQUID
|
LIQUID
|
||||||
|
|
||||||
expected = <<~OUTPUT
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<input type="text" />
|
<input type="text" />
|
||||||
|
|
||||||
<input type="password" value="pass" />
|
<input type="password" value="pass" />
|
||||||
OUTPUT
|
OUTPUT
|
||||||
|
|
||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
@@ -131,7 +150,12 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
def test_render_inline_snippet_empty_string_when_missing_argument
|
def test_render_inline_snippet_empty_string_when_missing_argument
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet "input" |type| %}
|
{% snippet input %}
|
||||||
|
{% doc %}
|
||||||
|
@param {string} type - Input type.
|
||||||
|
@param {string} value - Input value.
|
||||||
|
{% enddoc %}
|
||||||
|
|
||||||
<input type="{{ type }}" value="{{ value }}" />
|
<input type="{{ type }}" value="{{ value }}" />
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
@@ -139,6 +163,8 @@ class SnippetTest < Minitest::Test
|
|||||||
LIQUID
|
LIQUID
|
||||||
expected = <<~OUTPUT
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<input type="text" value="" />
|
<input type="text" value="" />
|
||||||
OUTPUT
|
OUTPUT
|
||||||
|
|
||||||
@@ -147,7 +173,12 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
def test_render_inline_snippet_shouldnt_leak_context
|
def test_render_inline_snippet_shouldnt_leak_context
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet "input" |type, value| %}
|
{% snippet input %}
|
||||||
|
{% doc %}
|
||||||
|
@param {string} type - Input type.
|
||||||
|
@param {string} value - Input value.
|
||||||
|
{% enddoc %}
|
||||||
|
|
||||||
<input type="{{ type }}" value="{{ value }}" />
|
<input type="{{ type }}" value="{{ value }}" />
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
@@ -158,6 +189,8 @@ class SnippetTest < Minitest::Test
|
|||||||
LIQUID
|
LIQUID
|
||||||
expected = <<~OUTPUT
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<input type="text" value="Hello" />
|
<input type="text" value="Hello" />
|
||||||
|
|
||||||
OUTPUT
|
OUTPUT
|
||||||
@@ -167,10 +200,15 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
def test_render_multiple_inline_snippets_without_leaking_context
|
def test_render_multiple_inline_snippets_without_leaking_context
|
||||||
template = <<~LIQUID.strip
|
template = <<~LIQUID.strip
|
||||||
{% snippet "input" |type| %}
|
{% snippet input %}
|
||||||
|
{% doc %}
|
||||||
|
@param {string} type - Input type.
|
||||||
|
{% enddoc %}
|
||||||
|
|
||||||
<input type="{{ type }}" />
|
<input type="{{ type }}" />
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
{% snippet "no_leak" %}
|
|
||||||
|
{% snippet no_leak %}
|
||||||
<input type="{{ type }}" />
|
<input type="{{ type }}" />
|
||||||
{% endsnippet %}
|
{% endsnippet %}
|
||||||
|
|
||||||
@@ -180,6 +218,9 @@ class SnippetTest < Minitest::Test
|
|||||||
expected = <<~OUTPUT
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<input type="text" />
|
<input type="text" />
|
||||||
|
|
||||||
<input type="" />
|
<input type="" />
|
||||||
@@ -187,4 +228,129 @@ class SnippetTest < Minitest::Test
|
|||||||
|
|
||||||
assert_template_result(expected, template)
|
assert_template_result(expected, template)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_parent_context_variable
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
|
{% snippet header %}
|
||||||
|
{% doc %}
|
||||||
|
@param {string} message - Message.
|
||||||
|
{% enddoc %}
|
||||||
|
|
||||||
|
<div class="header header--{{ color_scheme }}">
|
||||||
|
{{ message }}
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{%- render "header", message: "Welcome to my site" -%}
|
||||||
|
LIQUID
|
||||||
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="header header--dark">
|
||||||
|
Welcome to my site
|
||||||
|
</div>
|
||||||
|
OUTPUT
|
||||||
|
|
||||||
|
assert_template_result(expected, template)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_deeply_nested_snippets
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% assign color_scheme = 'first-color' %}
|
||||||
|
{% snippet first %}
|
||||||
|
{% assign color_scheme = 'second-color' %}
|
||||||
|
{% snippet second %}
|
||||||
|
{% assign color_scheme = 'third-color' %}
|
||||||
|
{% snippet third %}
|
||||||
|
<div class="header header--{{ color_scheme }}">
|
||||||
|
This is a header
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
{%- render "third" -%}
|
||||||
|
{% endsnippet %}
|
||||||
|
{%- render "second" -%}
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{%- render "first" -%}
|
||||||
|
LIQUID
|
||||||
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="header header--third-color">
|
||||||
|
This is a header
|
||||||
|
</div>
|
||||||
|
OUTPUT
|
||||||
|
|
||||||
|
assert_template_result(expected, template)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_snippet_with_variables_in_both_scopes
|
||||||
|
template = <<~LIQUID.strip
|
||||||
|
{% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
|
{% snippet header %}
|
||||||
|
{% assign color_scheme = 'light' %}
|
||||||
|
<div class="header header--{{ color_scheme }}">
|
||||||
|
{{ message }}
|
||||||
|
</div>
|
||||||
|
{% endsnippet %}
|
||||||
|
|
||||||
|
{{ color_scheme }}
|
||||||
|
|
||||||
|
{%- render "header", message: 'Welcome to my site' -%}
|
||||||
|
LIQUID
|
||||||
|
expected = <<~OUTPUT
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
dark
|
||||||
|
|
||||||
|
<div class="header header--light">
|
||||||
|
Welcome to my site
|
||||||
|
</div>
|
||||||
|
OUTPUT
|
||||||
|
|
||||||
|
assert_template_result(expected, template)
|
||||||
|
end
|
||||||
|
|
||||||
|
# def test_render_snippets_as_arguments
|
||||||
|
# template = <<~LIQUID.strip
|
||||||
|
# {% assign color_scheme = 'dark' %}
|
||||||
|
|
||||||
|
# {% snippet main_header %}
|
||||||
|
# {% assign color_scheme = 'auto' %}
|
||||||
|
|
||||||
|
# <div class="main main--{{ color_scheme }}">
|
||||||
|
# {%- render "header", message: 'Welcome to my site' -%}
|
||||||
|
# </div>
|
||||||
|
# {% endsnippet %}
|
||||||
|
|
||||||
|
# {% snippet header %}
|
||||||
|
# <div class="header header--{{ color_scheme }}">
|
||||||
|
# {{ message }}
|
||||||
|
# </div>
|
||||||
|
# {% endsnippet %}
|
||||||
|
|
||||||
|
# {%- render "main_header", header: header -%}
|
||||||
|
# LIQUID
|
||||||
|
# expected = <<~OUTPUT
|
||||||
|
# <div class="main main--auto">
|
||||||
|
# <div class="header header--auto">
|
||||||
|
# Welcome to my site
|
||||||
|
# </div>
|
||||||
|
# </div>
|
||||||
|
# OUTPUT
|
||||||
|
|
||||||
|
# assert_template_result(expected, template)
|
||||||
|
# end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user