mirror of
https://github.com/kemko/liquid.git
synced 2026-01-06 02:05:41 +03:00
Compare commits
3 Commits
experiment
...
template-f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d8f31046a9 | ||
|
|
6c6382ed69 | ||
|
|
53ba1372f9 |
@@ -15,6 +15,7 @@ matrix:
|
|||||||
name: Profiling Memory Usage
|
name: Profiling Memory Usage
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- rvm: ruby-head
|
- rvm: ruby-head
|
||||||
|
- rvm: 2.7
|
||||||
|
|
||||||
branches:
|
branches:
|
||||||
only:
|
only:
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ require 'liquid/partial_cache'
|
|||||||
require 'liquid/usage'
|
require 'liquid/usage'
|
||||||
require 'liquid/register'
|
require 'liquid/register'
|
||||||
require 'liquid/static_registers'
|
require 'liquid/static_registers'
|
||||||
|
require 'liquid/template_factory'
|
||||||
|
|
||||||
# Load all the tags of the standard library
|
# Load all the tags of the standard library
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -12,7 +12,10 @@ module Liquid
|
|||||||
|
|
||||||
parse_context.partial = true
|
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
|
cached_partials[template_name] = partial
|
||||||
ensure
|
ensure
|
||||||
parse_context.partial = false
|
parse_context.partial = false
|
||||||
|
|||||||
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
|
||||||
@@ -136,3 +136,16 @@ class StubFileSystem
|
|||||||
@values.fetch(template_path)
|
@values.fetch(template_path)
|
||||||
end
|
end
|
||||||
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.
|
# but measuring file reads is an OK proxy for this.
|
||||||
assert_equal(1, file_system.file_read_count)
|
assert_equal(1, file_system.file_read_count)
|
||||||
end
|
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
|
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