mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Merge pull request #425 from Shopify/pass-options-include
Pass options through on include
This commit is contained in:
@@ -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)
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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!
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user