mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40: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
|
||||
if (inline_snippet = context.registers[:inline_snippet][template_name])
|
||||
inner_context = context.new_isolated_subcontext
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,28 +1,25 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
# @liquid_public_docs
|
||||
# @liquid_type tag
|
||||
# @liquid_category theme
|
||||
# @liquid_name snippet
|
||||
# @liquid_summary
|
||||
# Creates a new inline snippet using a string value as the identifier.
|
||||
# Creates a new inline snippet.
|
||||
# @liquid_description
|
||||
# You can create inline snippets to make your Liquid code more modular.
|
||||
# @liquid_syntax
|
||||
# {% snippet "input" %}
|
||||
# {% snippet input %}
|
||||
# value
|
||||
# {% endsnippet %}
|
||||
|
||||
class Snippet < Block
|
||||
SYNTAX = /(#{QuotedString})(?:\s*\|\s*([\w\s,]+)\s*\|)?/o
|
||||
SYNTAX = /(#{VariableSignature}+)/o
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
|
||||
if markup =~ SYNTAX
|
||||
@to = Regexp.last_match(1)
|
||||
args = Regexp.last_match(2)
|
||||
|
||||
@args = args ? args.split(/\s*,\s*/) : []
|
||||
else
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.snippet")
|
||||
end
|
||||
@@ -30,22 +27,10 @@ module Liquid
|
||||
|
||||
def render(context)
|
||||
context.registers[:inline_snippet] ||= {}
|
||||
context.registers[:inline_snippet][snippet_id] = {
|
||||
body: snippet_body,
|
||||
args: @args,
|
||||
context.registers[:inline_snippet][@to] = {
|
||||
body: @body,
|
||||
}
|
||||
''
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def snippet_id
|
||||
@to[1, @to.size - 2]
|
||||
end
|
||||
|
||||
def snippet_body
|
||||
body = @body
|
||||
body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,7 +7,7 @@ class SnippetTest < Minitest::Test
|
||||
|
||||
def test_valid_inline_snippet
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet "input" %}
|
||||
{% snippet input %}
|
||||
Hey
|
||||
{% endsnippet %}
|
||||
LIQUID
|
||||
@@ -16,20 +16,9 @@ class SnippetTest < Minitest::Test
|
||||
assert_template_result(expected, template)
|
||||
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
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet "hey" %}
|
||||
{% snippet hey %}
|
||||
Hey
|
||||
{% endsnippet %}
|
||||
|
||||
@@ -45,11 +34,11 @@ class SnippetTest < Minitest::Test
|
||||
|
||||
def test_render_multiple_inline_snippets
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet "input" %}
|
||||
{% snippet input %}
|
||||
<input />
|
||||
{% endsnippet %}
|
||||
|
||||
{% snippet "banner" %}
|
||||
{% snippet banner %}
|
||||
<marquee direction="up" height="100px">
|
||||
Welcome to my store!
|
||||
</marquee>
|
||||
@@ -74,7 +63,7 @@ class SnippetTest < Minitest::Test
|
||||
|
||||
def test_render_inline_snippet_with_argument
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet "input" |type| %}
|
||||
{% snippet input %}
|
||||
<input type="{{ type }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
@@ -88,9 +77,36 @@ class SnippetTest < Minitest::Test
|
||||
assert_template_result(expected, template)
|
||||
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
|
||||
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 }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
@@ -98,6 +114,8 @@ class SnippetTest < Minitest::Test
|
||||
LIQUID
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<input type="text" value="Hello" />
|
||||
OUTPUT
|
||||
|
||||
@@ -106,24 +124,25 @@ class SnippetTest < Minitest::Test
|
||||
|
||||
def test_render_inline_snippets_using_same_argument_name
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet "input" |type| %}
|
||||
{% snippet input %}
|
||||
<input type="{{ type }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
{% snippet "inputs" |type, value| %}
|
||||
<input type="{{ type }}" value="{{ value }}" />
|
||||
{% snippet inputs %}
|
||||
<input type="{{ type }}" value="{{ value }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
{%- render "input", type: "text" -%}
|
||||
{%- render "inputs", type: "password", value: "pass" -%}
|
||||
LIQUID
|
||||
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<input type="text" />
|
||||
|
||||
<input type="password" value="pass" />
|
||||
<input type="password" value="pass" />
|
||||
OUTPUT
|
||||
|
||||
assert_template_result(expected, template)
|
||||
@@ -131,7 +150,12 @@ class SnippetTest < Minitest::Test
|
||||
|
||||
def test_render_inline_snippet_empty_string_when_missing_argument
|
||||
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 }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
@@ -139,6 +163,8 @@ class SnippetTest < Minitest::Test
|
||||
LIQUID
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<input type="text" value="" />
|
||||
OUTPUT
|
||||
|
||||
@@ -147,7 +173,12 @@ class SnippetTest < Minitest::Test
|
||||
|
||||
def test_render_inline_snippet_shouldnt_leak_context
|
||||
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 }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
@@ -158,6 +189,8 @@ class SnippetTest < Minitest::Test
|
||||
LIQUID
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
<input type="text" value="Hello" />
|
||||
|
||||
OUTPUT
|
||||
@@ -167,10 +200,15 @@ class SnippetTest < Minitest::Test
|
||||
|
||||
def test_render_multiple_inline_snippets_without_leaking_context
|
||||
template = <<~LIQUID.strip
|
||||
{% snippet "input" |type| %}
|
||||
{% snippet input %}
|
||||
{% doc %}
|
||||
@param {string} type - Input type.
|
||||
{% enddoc %}
|
||||
|
||||
<input type="{{ type }}" />
|
||||
{% endsnippet %}
|
||||
{% snippet "no_leak" %}
|
||||
|
||||
{% snippet no_leak %}
|
||||
<input type="{{ type }}" />
|
||||
{% endsnippet %}
|
||||
|
||||
@@ -180,6 +218,9 @@ class SnippetTest < Minitest::Test
|
||||
expected = <<~OUTPUT
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<input type="text" />
|
||||
|
||||
<input type="" />
|
||||
@@ -187,4 +228,129 @@ class SnippetTest < Minitest::Test
|
||||
|
||||
assert_template_result(expected, template)
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user