mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-16 09:20:42 -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
@@ -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