Pass options to include tags

This commit is contained in:
Tristan Hume
2014-08-26 10:50:25 -04:00
parent dfb6c20493
commit 68af2d6e2a
4 changed files with 41 additions and 3 deletions
+11 -1
View File
@@ -69,7 +69,7 @@ module Liquid
return cached return cached
end end
source = read_template_from_file_system(context) source = read_template_from_file_system(context)
partial = Liquid::Template.parse(source) 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
@@ -88,6 +88,16 @@ module Liquid
raise ArgumentError, "file_system.read_template_file expects two parameters: (template_name, context)" raise ArgumentError, "file_system.read_template_file expects two parameters: (template_name, context)"
end end
end end
def pass_options
dont_pass = @options[:include_options_blacklist]
return {locale: @options[:locale]} if dont_pass == true
opts = @options.merge(included: true, include_options_blacklist: false)
if dont_pass.is_a?(Array)
dont_pass.each {|o| opts.delete(o)}
end
opts
end
end end
Template.register_tag('include'.freeze, Include) Template.register_tag('include'.freeze, Include)
+3 -2
View File
@@ -103,7 +103,8 @@ module Liquid
# Parse source code. # Parse source code.
# Returns self for easy chaining # Returns self for easy chaining
def parse(source, options = {}) def parse(source, options = {})
@profiling = options.delete(:profile) @options = options
@profiling = options[:profile]
@root = Document.parse(tokenize(source), DEFAULT_OPTIONS.merge(options)) @root = Document.parse(tokenize(source), DEFAULT_OPTIONS.merge(options))
@warnings = nil @warnings = nil
self self
@@ -234,7 +235,7 @@ module Liquid
end end
def with_profiling def with_profiling
if @profiling if @profiling && !@options[:included]
@profiler = Profiler.new @profiler = Profiler.new
@profiler.start @profiler.start
+12
View File
@@ -48,6 +48,18 @@ class RenderProfilingTest < Minitest::Test
assert_equal 2, t.profiler[1].line_number assert_equal 2, t.profiler[1].line_number
end end
def test_profiling_includes_line_numbers_of_included_partials
t = Template.parse("{% include 'a_template' %}", :profile => true)
t.render!
included_children = t.profiler[0].children
# {% assign template_name = 'a_template' %}
assert_equal 1, included_children[0].line_number
# {{ template_name }}
assert_equal 2, included_children[1].line_number
end
def test_profiling_times_the_rendering_of_tokens def test_profiling_times_the_rendering_of_tokens
t = Template.parse("{% include 'a_template' %}", :profile => true) t = Template.parse("{% include 'a_template' %}", :profile => true)
t.render! t.render!
+15
View File
@@ -209,4 +209,19 @@ class IncludeTagTest < Minitest::Test
a.render! a.render!
assert_empty a.errors assert_empty a.errors
end end
def test_passing_options_to_included_templates
assert_raises(Liquid::SyntaxError) do
Template.parse("{% include template %}", error_mode: :strict).render!("template" => '{{ "X" || downcase }}')
end
with_error_mode(:lax) do
assert_equal 'x', Template.parse("{% include template %}", error_mode: :strict, include_options_blacklist: true).render!("template" => '{{ "X" || downcase }}')
end
assert_raises(Liquid::SyntaxError) do
Template.parse("{% include template %}", error_mode: :strict, include_options_blacklist: [:locale]).render!("template" => '{{ "X" || downcase }}')
end
with_error_mode(:lax) do
assert_equal 'x', Template.parse("{% include template %}", error_mode: :strict, include_options_blacklist: [:error_mode]).render!("template" => '{{ "X" || downcase }}')
end
end
end # IncludeTagTest end # IncludeTagTest