mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 16:25:42 +03:00
Compare commits
4 Commits
fix-consta
...
template-f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8f31046a9 | ||
|
|
6c6382ed69 | ||
|
|
53ba1372f9 | ||
|
|
57c9cf64eb |
@@ -15,6 +15,7 @@ matrix:
|
||||
name: Profiling Memory Usage
|
||||
allow_failures:
|
||||
- rvm: ruby-head
|
||||
- rvm: 2.7
|
||||
|
||||
branches:
|
||||
only:
|
||||
|
||||
@@ -80,6 +80,7 @@ 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,7 +12,10 @@ module Liquid
|
||||
|
||||
parse_context.partial = true
|
||||
|
||||
partial = Liquid::Template.parse(source, parse_context)
|
||||
template_factory = (context.registers[:template_factory] ||= Liquid::TemplateFactory.new)
|
||||
template = template_factory.for(template_name)
|
||||
|
||||
partial = template.parse(source, parse_context)
|
||||
cached_partials[template_name] = partial
|
||||
ensure
|
||||
parse_context.partial = false
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
module Liquid
|
||||
class Render < Tag
|
||||
SYNTAX = /(#{QuotedString}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||
FOR = 'for'
|
||||
SYNTAX = /(#{QuotedString}+)(\s+(with|#{FOR})\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||
|
||||
disable_tags "include"
|
||||
|
||||
@@ -14,11 +15,13 @@ module Liquid
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
|
||||
|
||||
template_name = Regexp.last_match(1)
|
||||
variable_name = Regexp.last_match(3)
|
||||
with_or_for = Regexp.last_match(3)
|
||||
variable_name = Regexp.last_match(4)
|
||||
|
||||
@alias_name = Regexp.last_match(5)
|
||||
@alias_name = Regexp.last_match(6)
|
||||
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
|
||||
@template_name_expr = Expression.parse(template_name)
|
||||
@for = (with_or_for == FOR)
|
||||
|
||||
@attributes = {}
|
||||
markup.scan(TagAttributes) do |key, value|
|
||||
@@ -58,7 +61,7 @@ module Liquid
|
||||
}
|
||||
|
||||
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
||||
if variable.is_a?(Array)
|
||||
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) }
|
||||
else
|
||||
|
||||
9
lib/liquid/template_factory.rb
Normal file
9
lib/liquid/template_factory.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Liquid
|
||||
class TemplateFactory
|
||||
def for(_template_name)
|
||||
Liquid::Template.new
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -214,4 +214,22 @@ class RenderTagTest < Minitest::Test
|
||||
assert_template_result("Product: Draft 151cm first index:1 Product: Element 155cm last index:2 ",
|
||||
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||
end
|
||||
|
||||
def test_render_tag_for_drop
|
||||
Liquid::Template.file_system = StubFileSystem.new(
|
||||
'loop' => "{{ value.foo }}",
|
||||
)
|
||||
|
||||
assert_template_result("123",
|
||||
"{% render 'loop' for loop as value %}", "loop" => TestEnumerable.new)
|
||||
end
|
||||
|
||||
def test_render_tag_with_drop
|
||||
Liquid::Template.file_system = StubFileSystem.new(
|
||||
'loop' => "{{ value }}",
|
||||
)
|
||||
|
||||
assert_template_result("TestEnumerable",
|
||||
"{% render 'loop' with loop as value %}", "loop" => TestEnumerable.new)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -136,3 +136,16 @@ 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,4 +90,39 @@ 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
|
||||
|
||||
12
test/unit/template_factory_unit_test.rb
Normal file
12
test/unit/template_factory_unit_test.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
# 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