Skip to content

Commit

Permalink
snippet using inner context
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilGenius13 committed Sep 30, 2024
1 parent 404d716 commit 447347c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 52 deletions.
25 changes: 8 additions & 17 deletions example/server/templates/index.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -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' %}
Expand Down
10 changes: 8 additions & 2 deletions lib/liquid/tags/render.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,18 @@ def render_tag(context, output)
# 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(
Expand Down
15 changes: 9 additions & 6 deletions lib/liquid/tags/snippet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,28 @@ module Liquid
# value
# {% endsnippet %}
class Snippet < Block
SYNTAX = /(#{QuotedString}+) +\|(#{VariableSegment}*)\|/o
# 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
end

def render(context)
context.registers[:inline_snippet] ||= {}
context.registers[:inline_snippet][snippet_id] = snippet_body
# context.registers[:inline_snippet][snippet_id] = snippet_body
context.registers[:inline_snippet][snippet_id] = {
body: snippet_body,
args: @args,
}
''
end

Expand Down
70 changes: 43 additions & 27 deletions test/integration/tags/snippet_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def test_render_multiple_inline_snippets
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 }}" />
Expand All @@ -89,38 +88,55 @@ def test_render_inline_snippet_with_argument
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
<input type="text" value="Hello" />
OUTPUT

assert_template_result(expected, template)
end

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" -%}
# LIQUID
# expected = <<~OUTPUT
{%- render "input", type: "text" -%}
LIQUID
expected = <<~OUTPUT
# <input type="text" value="Hello" />
# OUTPUT
<input type="text" value="" />
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_shouldnt_leak_context
# template = <<~LIQUID.strip
# {% snippet "input" |type, value| %}
# <input type="{{ type }}" value="{{ value }}" />
# {% endsnippet %}
{%- render "input", type: "text", value: "Hello" -%}
# {%- render "input", type: "text", value: "Hello" -%}
{{ type }}
{{ value }}
LIQUID
expected = <<~OUTPUT
# {{ type }}
# {{ value }}
# LIQUID
# expected = <<~OUTPUT
<input type="text" value="Hello" />
# <input type="text" value="Hello" />
# OUTPUT
OUTPUT

# assert_template_result(expected, template)
# end
assert_template_result(expected, template)
end
end

0 comments on commit 447347c

Please sign in to comment.