mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-18 02:10:41 -07:00
snippet using inner context
This commit is contained in:
@@ -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(
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user