mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 16:25:42 +03:00
Compare commits
2 Commits
template-f
...
sfr-verifi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
974080c2cf | ||
|
|
e52475a7cd |
@@ -15,7 +15,6 @@ matrix:
|
||||
name: Profiling Memory Usage
|
||||
allow_failures:
|
||||
- rvm: ruby-head
|
||||
- rvm: 2.7
|
||||
|
||||
branches:
|
||||
only:
|
||||
|
||||
@@ -80,7 +80,6 @@ require 'liquid/partial_cache'
|
||||
require 'liquid/usage'
|
||||
require 'liquid/register'
|
||||
require 'liquid/static_registers'
|
||||
require 'liquid/template_factory'
|
||||
|
||||
# Load all the tags of the standard library
|
||||
#
|
||||
|
||||
@@ -12,10 +12,7 @@ module Liquid
|
||||
|
||||
parse_context.partial = true
|
||||
|
||||
template_factory = (context.registers[:template_factory] ||= Liquid::TemplateFactory.new)
|
||||
template = template_factory.for(template_name)
|
||||
|
||||
partial = template.parse(source, parse_context)
|
||||
partial = Liquid::Template.parse(source, parse_context)
|
||||
cached_partials[template_name] = partial
|
||||
ensure
|
||||
parse_context.partial = false
|
||||
|
||||
@@ -50,19 +50,7 @@ module Liquid
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
||||
|
||||
partial = PartialCache.load(
|
||||
template_name,
|
||||
context: context,
|
||||
parse_context: parse_context
|
||||
)
|
||||
|
||||
context_variable_name = @alias_name || template_name.split('/').last
|
||||
|
||||
variable = if @variable_name_expr
|
||||
context.evaluate(@variable_name_expr)
|
||||
else
|
||||
context.find_variable(template_name, raise_on_not_found: false)
|
||||
end
|
||||
partial = load_partial(template_name, context, parse_context)
|
||||
|
||||
old_template_name = context.template_name
|
||||
old_partial = context.partial
|
||||
@@ -71,7 +59,15 @@ module Liquid
|
||||
context.partial = true
|
||||
context.stack do
|
||||
@attributes.each do |key, value|
|
||||
context[key] = context.evaluate(value)
|
||||
context[key] = evaluate(context, value)
|
||||
end
|
||||
|
||||
context_variable_name = @alias_name || template_name.split('/').last
|
||||
|
||||
variable = if @variable_name_expr
|
||||
evaluate(context, @variable_name_expr)
|
||||
else
|
||||
find_variable(context, template_name, raise_on_not_found: false)
|
||||
end
|
||||
|
||||
if variable.is_a?(Array)
|
||||
@@ -103,6 +99,24 @@ module Liquid
|
||||
] + @node.attributes.values
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def evaluate(context, value)
|
||||
context.evaluate(value)
|
||||
end
|
||||
|
||||
def find_variable(context, *args)
|
||||
context.find_variable(*args)
|
||||
end
|
||||
|
||||
def load_partial(template_name, context, parse_context)
|
||||
PartialCache.load(
|
||||
template_name,
|
||||
context: context,
|
||||
parse_context: parse_context
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('include', Include)
|
||||
|
||||
@@ -38,11 +38,7 @@ module Liquid
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
||||
|
||||
partial = PartialCache.load(
|
||||
template_name,
|
||||
context: context,
|
||||
parse_context: parse_context
|
||||
)
|
||||
partial = load_partial(template_name, context, parse_context)
|
||||
|
||||
context_variable_name = @alias_name || template_name.split('/').last
|
||||
|
||||
@@ -53,14 +49,14 @@ module Liquid
|
||||
inner_context['forloop'] = forloop if forloop
|
||||
|
||||
@attributes.each do |key, value|
|
||||
inner_context[key] = context.evaluate(value)
|
||||
inner_context[key] = evaluate(context, value)
|
||||
end
|
||||
inner_context[context_variable_name] = var unless var.nil?
|
||||
partial.render_to_output_buffer(inner_context, output)
|
||||
forloop&.send(:increment!)
|
||||
}
|
||||
|
||||
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
||||
variable = @variable_name_expr ? evaluate(context, @variable_name_expr) : nil
|
||||
if @for && variable.respond_to?(:each) && variable.respond_to?(:count)
|
||||
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
|
||||
variable.each { |var| render_partial_func.call(var, forloop) }
|
||||
@@ -78,6 +74,20 @@ module Liquid
|
||||
] + @node.attributes.values
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def evaluate(context, value)
|
||||
context.evaluate(value)
|
||||
end
|
||||
|
||||
def load_partial(template_name, context, parse_context)
|
||||
PartialCache.load(
|
||||
template_name,
|
||||
context: context,
|
||||
parse_context: parse_context
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('render', Render)
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
class TemplateFactory
|
||||
def for(_template_name)
|
||||
Liquid::Template.new
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -136,16 +136,3 @@ class StubFileSystem
|
||||
@values.fetch(template_path)
|
||||
end
|
||||
end
|
||||
|
||||
class StubTemplateFactory
|
||||
attr_reader :count
|
||||
|
||||
def initialize
|
||||
@count = 0
|
||||
end
|
||||
|
||||
def for(_template_name)
|
||||
@count += 1
|
||||
Liquid::Template.new
|
||||
end
|
||||
end
|
||||
|
||||
@@ -90,39 +90,4 @@ class PartialCacheUnitTest < Minitest::Test
|
||||
# but measuring file reads is an OK proxy for this.
|
||||
assert_equal(1, file_system.file_read_count)
|
||||
end
|
||||
|
||||
def test_uses_default_template_factory_when_no_template_factory_found_in_register
|
||||
context = Liquid::Context.build(
|
||||
registers: {
|
||||
file_system: StubFileSystem.new('my_partial' => 'my partial body'),
|
||||
}
|
||||
)
|
||||
|
||||
partial = Liquid::PartialCache.load(
|
||||
'my_partial',
|
||||
context: context,
|
||||
parse_context: Liquid::ParseContext.new
|
||||
)
|
||||
|
||||
assert_equal('my partial body', partial.render)
|
||||
end
|
||||
|
||||
def test_uses_template_factory_register_if_present
|
||||
template_factory = StubTemplateFactory.new
|
||||
context = Liquid::Context.build(
|
||||
registers: {
|
||||
file_system: StubFileSystem.new('my_partial' => 'my partial body'),
|
||||
template_factory: template_factory,
|
||||
}
|
||||
)
|
||||
|
||||
partial = Liquid::PartialCache.load(
|
||||
'my_partial',
|
||||
context: context,
|
||||
parse_context: Liquid::ParseContext.new
|
||||
)
|
||||
|
||||
assert_equal('my partial body', partial.render)
|
||||
assert_equal(1, template_factory.count)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
|
||||
class TemplateFactoryUnitTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_for_returns_liquid_template_instance
|
||||
template = TemplateFactory.new.for("anything")
|
||||
assert_instance_of(Liquid::Template, template)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user