Compare commits

...
Author SHA1 Message Date
Ian Ker-Seymer 62c9cd88e1 Make partial_cache a configurable option 2024-09-23 15:40:49 -04:00
6 changed files with 25 additions and 10 deletions
+2 -1
View File
@@ -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
+6 -1
View File
@@ -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.
+3 -1
View File
@@ -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,
+2 -1
View File
@@ -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,
+10 -4
View File
@@ -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
View File
@@ -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)