mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-13 07:50:43 -07:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
62c9cd88e1 |
@@ -19,7 +19,7 @@ module Liquid
|
||||
|
||||
# rubocop:disable Metrics/ParameterLists
|
||||
def self.build(environment: Environment.default, environments: {}, outer_scope: {}, registers: {}, rethrow_errors: false, resource_limits: nil, static_environments: {}, &block)
|
||||
new(environments, outer_scope, registers, rethrow_errors, resource_limits, static_environments, &block)
|
||||
new(environments, outer_scope, registers, rethrow_errors, resource_limits, static_environments, environment, &block)
|
||||
end
|
||||
|
||||
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil, static_environments = {}, environment = Environment.default)
|
||||
@@ -41,6 +41,7 @@ module Liquid
|
||||
@disabled_tags = {}
|
||||
|
||||
@registers.static[:cached_partials] ||= {}
|
||||
@registers.static[:partial_cache] ||= environment.partial_cache
|
||||
@registers.static[:file_system] ||= environment.file_system
|
||||
@registers.static[:template_factory] ||= Liquid::TemplateFactory.new
|
||||
|
||||
|
||||
@@ -26,6 +26,9 @@ module Liquid
|
||||
# template can consume.
|
||||
attr_accessor :default_resource_limits
|
||||
|
||||
# Implementation for partial caching must respond to `load`
|
||||
attr_accessor :partial_cache
|
||||
|
||||
class << self
|
||||
# Creates a new environment instance.
|
||||
#
|
||||
@@ -39,12 +42,13 @@ module Liquid
|
||||
# render exceptions.
|
||||
# @yieldparam environment [Environment] The environment instance that is being built.
|
||||
# @return [Environment] The new environment instance.
|
||||
def build(tags: nil, file_system: nil, error_mode: nil, exception_renderer: nil)
|
||||
def build(tags: nil, file_system: nil, error_mode: nil, exception_renderer: nil, partial_cache: nil)
|
||||
ret = new
|
||||
ret.tags = Template::TagRegistry.new(tags) if tags
|
||||
ret.file_system = file_system if file_system
|
||||
ret.error_mode = error_mode if error_mode
|
||||
ret.exception_renderer = exception_renderer if exception_renderer
|
||||
ret.partial_cache = partial_cache if partial_cache
|
||||
yield ret if block_given?
|
||||
ret.freeze
|
||||
end
|
||||
@@ -83,6 +87,7 @@ module Liquid
|
||||
@file_system = BlankFileSystem.new
|
||||
@default_resource_limits = Const::EMPTY_HASH
|
||||
@strainer_template_class_cache = {}
|
||||
@partial_cache = PartialCache
|
||||
end
|
||||
|
||||
# Registers a new tag with the environment.
|
||||
|
||||
@@ -54,7 +54,9 @@ module Liquid
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name.is_a?(String)
|
||||
|
||||
partial = PartialCache.load(
|
||||
partial_cache = context.registers[:partial_cache]
|
||||
|
||||
partial = partial_cache.load(
|
||||
template_name,
|
||||
context: context,
|
||||
parse_context: parse_context,
|
||||
|
||||
@@ -66,7 +66,8 @@ module Liquid
|
||||
template_name = @template_name_expr
|
||||
raise ::ArgumentError unless template_name.is_a?(String)
|
||||
|
||||
partial = PartialCache.load(
|
||||
partial_cache = context.registers[:partial_cache]
|
||||
partial = partial_cache.load(
|
||||
template_name,
|
||||
context: context,
|
||||
parse_context: parse_context,
|
||||
|
||||
@@ -22,11 +22,17 @@ class RenderTagTest < Minitest::Test
|
||||
end
|
||||
|
||||
def test_render_passes_named_arguments_into_inner_scope
|
||||
partial_cache = Class.new do
|
||||
def load(template_name, context:, parse_context:)
|
||||
Liquid::Template.parse("my own partial cache (#{template_name})")
|
||||
end
|
||||
end.new
|
||||
environment = Liquid::Environment.build(partial_cache: partial_cache)
|
||||
|
||||
assert_template_result(
|
||||
'My Product',
|
||||
'{% render "product", inner_product: outer_product %}',
|
||||
{ 'outer_product' => { 'title' => 'My Product' } },
|
||||
partials: { 'product' => '{{ inner_product.title }}' },
|
||||
'my own partial cache (my_partial.liquid)',
|
||||
'{% render "my_partial.liquid" %}',
|
||||
environment: environment,
|
||||
)
|
||||
end
|
||||
|
||||
|
||||
+2
-2
@@ -40,10 +40,10 @@ module Minitest
|
||||
def assert_template_result(
|
||||
expected, template, assigns = {},
|
||||
message: nil, partials: nil, error_mode: nil, render_errors: false,
|
||||
template_factory: nil
|
||||
template_factory: nil, environment: nil
|
||||
)
|
||||
file_system = StubFileSystem.new(partials || {})
|
||||
environment = Liquid::Environment.build(file_system: file_system)
|
||||
environment ||= Liquid::Environment.build(file_system: file_system)
|
||||
template = Liquid::Template.parse(template, line_numbers: true, error_mode: error_mode&.to_sym, environment: environment)
|
||||
registers = Liquid::Registers.new(file_system: file_system, template_factory: template_factory)
|
||||
context = Liquid::Context.build(static_environments: assigns, rethrow_errors: !render_errors, registers: registers, environment: environment)
|
||||
|
||||
Reference in New Issue
Block a user