snippet using inner context

This commit is contained in:
Josh Faigan
2024-09-30 15:58:00 -04:00
parent 404d71613c
commit 3399981b89
4 changed files with 89 additions and 52 deletions
+6 -15
View File
@@ -35,29 +35,20 @@
{% snippet "main" %} {% snippet "main" %}
{% # Snippet input %} {% # Snippet input %}
{% snippet "input" %} {% snippet "input" |type, name| %}
<div> <div>
<label>{{ label }}</label> <label>{{ type | capitalize }}</label>
<input type={{ type }}> <input type={{ type }}>
</div> </div>
{% endsnippet %} {% endsnippet %}
{% snippet "league" %} {% snippet "league" %}
<h1>Welcome to the {{ team }} of super {{ name }}</h1> <h1>Welcome to the league of super evil</h1>
{% endsnippet %} {% endsnippet %}
{% render "league" %}
{% # Snippet banner %} {% render "input", type: "text" %}
{% snippet "banner" %} {% render "input", type: "password" %}
<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' %}
{% endsnippet %} {% endsnippet %}
{% render 'main' %} {% render 'main' %}
+7 -1
View File
@@ -69,12 +69,18 @@ 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
# 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| @attributes.each do |key, value|
if snippet_args.include?(key)
inner_context[key] = context.evaluate(value) inner_context[key] = context.evaluate(value)
end end
end
return output << inline_snippet.render(inner_context) return output << snippet_body.render(inner_context)
end end
partial = PartialCache.load( partial = PartialCache.load(
+7 -6
View File
@@ -14,17 +14,15 @@ module Liquid
# value # value
# {% endsnippet %} # {% endsnippet %}
class Snippet < Block class Snippet < Block
SYNTAX = /(#{QuotedString}+) +\|(#{VariableSegment}*)\|/o SYNTAX = /(#{QuotedString})(?:\s*\|\s*([\w\s,]+)\s*\|)?/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 = Regexp.last_match(2)
@args = [] @args = args ? args.split(/\s*,\s*/) : []
@args << arg if arg
else else
raise SyntaxError, options[:locale].t("errors.syntax.snippet") raise SyntaxError, options[:locale].t("errors.syntax.snippet")
end end
@@ -32,7 +30,10 @@ module Liquid
def render(context) def render(context)
context.registers[:inline_snippet] ||= {} context.registers[:inline_snippet] ||= {}
context.registers[:inline_snippet][snippet_id] = snippet_body context.registers[:inline_snippet][snippet_id] = {
body: snippet_body,
args: @args,
}
'' ''
end end
+66 -27
View File
@@ -73,7 +73,6 @@ class SnippetTest < Minitest::Test
end end
def test_render_inline_snippet_with_argument def test_render_inline_snippet_with_argument
# This passes whether or not we have the new or old SYNTAX
template = <<~LIQUID.strip template = <<~LIQUID.strip
{% snippet "input" |type| %} {% snippet "input" |type| %}
<input type="{{ type }}" /> <input type="{{ type }}" />
@@ -89,38 +88,78 @@ class SnippetTest < Minitest::Test
assert_template_result(expected, template) assert_template_result(expected, template)
end 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" |type, value| %}
# <input type="{{ type }}" value="{{ value }}" /> <input type="{{ type }}" value="{{ value }}" />
# {% endsnippet %} {% endsnippet %}
# {%- render "input", type: "text", value: "Hello" -%} {%- render "input", type: "text", value: "Hello" -%}
# LIQUID LIQUID
# expected = <<~OUTPUT expected = <<~OUTPUT
# <input type="text" value="Hello" /> <input type="text" value="Hello" />
# OUTPUT OUTPUT
# assert_template_result(expected, template) assert_template_result(expected, template)
# end end
# def test_render_inline_snippet_shouldnt_leak_context def test_render_inline_snippet_empty_string_when_missing_argument
# template = <<~LIQUID.strip template = <<~LIQUID.strip
# {% snippet "input" |type, value| %} {% snippet "input" |type| %}
# <input type="{{ type }}" value="{{ value }}" /> <input type="{{ type }}" value="{{ value }}" />
# {% endsnippet %} {% endsnippet %}
# {%- render "input", type: "text", value: "Hello" -%} {%- render "input", type: "text" -%}
LIQUID
expected = <<~OUTPUT
# {{ type }} <input type="text" value="" />
# {{ value }} OUTPUT
# LIQUID
# expected = <<~OUTPUT
# <input type="text" value="Hello" /> assert_template_result(expected, template)
# OUTPUT end
# assert_template_result(expected, template) def test_render_inline_snippet_shouldnt_leak_context
# end 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 end