mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Create SnippetDrop and set in scope
This commit is contained in:
committed by
Guilherme Carreiro
parent
1eca707c4a
commit
12bbbc4537
@@ -67,6 +67,7 @@ require 'liquid/i18n'
|
||||
require 'liquid/drop'
|
||||
require 'liquid/tablerowloop_drop'
|
||||
require 'liquid/forloop_drop'
|
||||
require 'liquid/snippet_drop'
|
||||
require 'liquid/extensions'
|
||||
require 'liquid/errors'
|
||||
require 'liquid/interrupts'
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
class SnippetDrop < Drop
|
||||
attr_reader :body
|
||||
|
||||
def initialize(body)
|
||||
super()
|
||||
@body = body
|
||||
end
|
||||
|
||||
def to_s
|
||||
'SnippetDrop'
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -42,6 +42,10 @@ module Liquid
|
||||
@is_for_loop
|
||||
end
|
||||
|
||||
def inherit_context?
|
||||
@inherit_context
|
||||
end
|
||||
|
||||
def render_to_output_buffer(context, output)
|
||||
render_tag(context, output)
|
||||
end
|
||||
@@ -51,14 +55,15 @@ module Liquid
|
||||
template_name = @template_name_expr
|
||||
raise ::ArgumentError unless template_name.is_a?(String)
|
||||
|
||||
# Inline snippets take precedence over external snippets
|
||||
if (inline_snippet = context.registers[:inline_snippet][template_name])
|
||||
if context[template_name].is_a?(Liquid::SnippetDrop)
|
||||
snippet_drop = context[template_name]
|
||||
inner_context = context.new_isolated_subcontext
|
||||
snippet_body = inline_snippet[:body]
|
||||
|
||||
context.scopes.each do |scope|
|
||||
scope.each do |key, value|
|
||||
inner_context[key] = value
|
||||
if inherit_context?
|
||||
context.scopes.each do |scope|
|
||||
scope.each do |key, value|
|
||||
inner_context[key] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -66,7 +71,7 @@ module Liquid
|
||||
inner_context[key] = context.evaluate(value)
|
||||
end
|
||||
|
||||
return output << snippet_body.render(inner_context)
|
||||
return output << snippet_drop.body.render(inner_context)
|
||||
end
|
||||
|
||||
partial = PartialCache.load(
|
||||
|
||||
@@ -25,12 +25,14 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
def render(context)
|
||||
context.registers[:inline_snippet] ||= {}
|
||||
context.registers[:inline_snippet][@to] = {
|
||||
body: @body,
|
||||
}
|
||||
''
|
||||
def render_to_output_buffer(context, output)
|
||||
snippet_drop = SnippetDrop.new(@body)
|
||||
context.scopes.last[@to] = snippet_drop
|
||||
output
|
||||
end
|
||||
|
||||
def blank?
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user