mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 16:25:42 +03:00
Compare commits
4 Commits
render-for
...
include_pa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5077463ec6 | ||
|
|
b765343ee2 | ||
|
|
cbd1a07c1c | ||
|
|
a5c1f7b6f5 |
@@ -30,6 +30,11 @@ module Liquid
|
|||||||
@attributes[key] = value
|
@attributes[key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if partial_parsable_at_parse_time?
|
||||||
|
source = read_template_from_file_system_at_parse
|
||||||
|
@partial = Liquid::Template.parse(source, pass_options)
|
||||||
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
raise SyntaxError.new(options[:locale].t("errors.syntax.include".freeze))
|
raise SyntaxError.new(options[:locale].t("errors.syntax.include".freeze))
|
||||||
end
|
end
|
||||||
@@ -66,27 +71,40 @@ module Liquid
|
|||||||
template_name = context[@template_name]
|
template_name = context[@template_name]
|
||||||
|
|
||||||
if cached = cached_partials[template_name]
|
if cached = cached_partials[template_name]
|
||||||
return cached
|
cached
|
||||||
|
else
|
||||||
|
if @partial
|
||||||
|
partial = @partial
|
||||||
|
else
|
||||||
|
partial = Liquid::Template.parse(read_template_from_file_system(context), pass_options)
|
||||||
end
|
end
|
||||||
source = read_template_from_file_system(context)
|
|
||||||
partial = Liquid::Template.parse(source, pass_options)
|
|
||||||
cached_partials[template_name] = partial
|
cached_partials[template_name] = partial
|
||||||
context.registers[:cached_partials] = cached_partials
|
context.registers[:cached_partials] = cached_partials
|
||||||
partial
|
partial
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def read_template_from_file_system(context)
|
def read_template_from_file_system(context)
|
||||||
file_system = context.registers[:file_system] || Liquid::Template.file_system
|
file_system = context.registers[:file_system] || Liquid::Template.file_system
|
||||||
|
|
||||||
# make read_template_file call backwards-compatible.
|
|
||||||
case file_system.method(:read_template_file).arity
|
|
||||||
when 1
|
|
||||||
file_system.read_template_file(context[@template_name])
|
file_system.read_template_file(context[@template_name])
|
||||||
when 2
|
|
||||||
file_system.read_template_file(context[@template_name], context)
|
|
||||||
else
|
|
||||||
raise ArgumentError, "file_system.read_template_file expects two parameters: (template_name, context)"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def read_template_from_file_system_at_parse
|
||||||
|
parsed_file_system.read_template_file(parsed_template_name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def parsed_file_system
|
||||||
|
options[:file_system]
|
||||||
|
end
|
||||||
|
|
||||||
|
def partial_parsable_at_parse_time?
|
||||||
|
template_name_is_string_constant = parsed_template_name.is_a?(String)
|
||||||
|
options[:file_system] && template_name_is_string_constant
|
||||||
|
end
|
||||||
|
|
||||||
|
def parsed_template_name
|
||||||
|
Expression.parse(@template_name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def pass_options
|
def pass_options
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ class FoobarTag < Liquid::Tag
|
|||||||
end
|
end
|
||||||
|
|
||||||
class BlankTestFileSystem
|
class BlankTestFileSystem
|
||||||
def read_template_file(template_path, context)
|
def read_template_file(template_path)
|
||||||
template_path
|
template_path
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ class RenderProfilingTest < Minitest::Test
|
|||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
class ProfilingFileSystem
|
class ProfilingFileSystem
|
||||||
def read_template_file(template_path, context)
|
def read_template_file(template_path)
|
||||||
"Rendering template {% assign template_name = '#{template_path}'%}\n{{ template_name }}"
|
"Rendering template {% assign template_name = '#{template_path}'%}\n{{ template_name }}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
require 'test_helper'
|
require 'test_helper'
|
||||||
|
|
||||||
class TestFileSystem
|
class TestFileSystem
|
||||||
def read_template_file(template_path, context)
|
def read_template_file(template_path)
|
||||||
case template_path
|
case template_path
|
||||||
when "product"
|
when "product"
|
||||||
"Product: {{ product.title }} "
|
"Product: {{ product.title }} "
|
||||||
@@ -33,15 +33,22 @@ class TestFileSystem
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# FileSystems at parse time don't have a context
|
||||||
|
class ParseFileSystem
|
||||||
|
def read_template_file(template_path)
|
||||||
|
'from ParseFileSystem'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class OtherFileSystem
|
class OtherFileSystem
|
||||||
def read_template_file(template_path, context)
|
def read_template_file(template_path)
|
||||||
'from OtherFileSystem'
|
'from OtherFileSystem'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class CountingFileSystem
|
class CountingFileSystem
|
||||||
attr_reader :count
|
attr_reader :count
|
||||||
def read_template_file(template_path, context)
|
def read_template_file(template_path)
|
||||||
@count ||= 0
|
@count ||= 0
|
||||||
@count += 1
|
@count += 1
|
||||||
'from CountingFileSystem'
|
'from CountingFileSystem'
|
||||||
@@ -77,6 +84,15 @@ class IncludeTagTest < Minitest::Test
|
|||||||
Template.parse("{% include 'pick_a_source' %}").render!({}, :registers => {:file_system => OtherFileSystem.new})
|
Template.parse("{% include 'pick_a_source' %}").render!({}, :registers => {:file_system => OtherFileSystem.new})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_include_tag_can_use_file_system_at_parse_so_it_can_be_parsed_before_rendered
|
||||||
|
assert_equal 'from ParseFileSystem',
|
||||||
|
Template.parse("{% include 'pick_a_source' %}",file_system: ParseFileSystem.new).render!({}, :registers => {:file_system => OtherFileSystem.new})
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_include_tag_use_registers_file_system_when_it_cant_be_preparse
|
||||||
|
assert_equal 'from OtherFileSystem',
|
||||||
|
Template.parse("{% include template %}",file_system: ParseFileSystem.new).render!({}, :registers => {:file_system => OtherFileSystem.new})
|
||||||
|
end
|
||||||
|
|
||||||
def test_include_tag_with
|
def test_include_tag_with
|
||||||
assert_template_result "Product: Draft 151cm ",
|
assert_template_result "Product: Draft 151cm ",
|
||||||
@@ -125,7 +141,7 @@ class IncludeTagTest < Minitest::Test
|
|||||||
def test_recursively_included_template_does_not_produce_endless_loop
|
def test_recursively_included_template_does_not_produce_endless_loop
|
||||||
|
|
||||||
infinite_file_system = Class.new do
|
infinite_file_system = Class.new do
|
||||||
def read_template_file(template_path, context)
|
def read_template_file(template_path)
|
||||||
"-{% include 'loop' %}"
|
"-{% include 'loop' %}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -138,18 +154,6 @@ class IncludeTagTest < Minitest::Test
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_backwards_compatability_support_for_overridden_read_template_file
|
|
||||||
infinite_file_system = Class.new do
|
|
||||||
def read_template_file(template_path) # testing only one argument here.
|
|
||||||
"- hi mom"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
Liquid::Template.file_system = infinite_file_system.new
|
|
||||||
|
|
||||||
Template.parse("{% include 'hi_mom' %}").render!
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_dynamically_choosen_template
|
def test_dynamically_choosen_template
|
||||||
assert_template_result "Test123", "{% include template %}", "template" => 'Test123'
|
assert_template_result "Test123", "{% include template %}", "template" => 'Test123'
|
||||||
assert_template_result "Test321", "{% include template %}", "template" => 'Test321'
|
assert_template_result "Test321", "{% include template %}", "template" => 'Test321'
|
||||||
|
|||||||
Reference in New Issue
Block a user