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
+8 -2
View File
@@ -69,12 +69,18 @@ module Liquid
# 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(
+7 -6
View File
@@ -14,17 +14,15 @@ module Liquid
# value
# {% endsnippet %}
class Snippet < Block
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
@@ -32,7 +30,10 @@ module Liquid
def render(context)
context.registers[:inline_snippet] ||= {}
context.registers[:inline_snippet][snippet_id] = snippet_body
context.registers[:inline_snippet][snippet_id] = {
body: snippet_body,
args: @args,
}
''
end