diff --git a/lib/liquid/context.rb b/lib/liquid/context.rb index 01123564..20a94e66 100644 --- a/lib/liquid/context.rb +++ b/lib/liquid/context.rb @@ -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 diff --git a/lib/liquid/environment.rb b/lib/liquid/environment.rb index fdd404fc..583c8e3b 100644 --- a/lib/liquid/environment.rb +++ b/lib/liquid/environment.rb @@ -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. diff --git a/lib/liquid/tags/include.rb b/lib/liquid/tags/include.rb index b4f1be13..2035bd5c 100644 --- a/lib/liquid/tags/include.rb +++ b/lib/liquid/tags/include.rb @@ -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, diff --git a/lib/liquid/tags/render.rb b/lib/liquid/tags/render.rb index 3615b1b3..7cfaca0e 100644 --- a/lib/liquid/tags/render.rb +++ b/lib/liquid/tags/render.rb @@ -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, diff --git a/test/integration/tags/render_tag_test.rb b/test/integration/tags/render_tag_test.rb index 01485cfa..a4e872ad 100644 --- a/test/integration/tags/render_tag_test.rb +++ b/test/integration/tags/render_tag_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index e5c88fb2..81882347 100755 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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)