diff --git a/lib/liquid/file_system.rb b/lib/liquid/file_system.rb index 31f5dd2..57363c3 100644 --- a/lib/liquid/file_system.rb +++ b/lib/liquid/file_system.rb @@ -14,7 +14,7 @@ module Liquid # This will parse the template with a LocalFileSystem implementation rooted at 'template_path'. class BlankFileSystem # Called by Liquid to retrieve a template file - def read_template_file(context, template_name) + def read_template_file(template_path, context) raise FileSystemError, "This liquid context does not allow includes." end end @@ -38,8 +38,7 @@ module Liquid @root = root end - def read_template_file(context, template_name) - template_path = context[template_name] + def read_template_file(template_path, context) full_path = full_path(template_path) raise FileSystemError, "No such template '#{template_path}'" unless File.exists?(full_path) diff --git a/lib/liquid/tags/include.rb b/lib/liquid/tags/include.rb index 8bee9fa..221e797 100644 --- a/lib/liquid/tags/include.rb +++ b/lib/liquid/tags/include.rb @@ -22,12 +22,10 @@ module Liquid def parse(tokens) end - + def render(context) - file_system = context.registers[:file_system] || Liquid::Template.file_system - source = file_system.read_template_file(context, @template_name) + source = _read_template_from_file_system(context) partial = Liquid::Template.parse(source) - variable = context[@variable_name || @template_name[1..-2]] context.stack do @@ -50,6 +48,21 @@ module Liquid end end end + + private + def _read_template_from_file_system(context) + 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]) + 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 end Template.register_tag('include', Include) diff --git a/test/lib/liquid/file_system_test.rb b/test/lib/liquid/file_system_test.rb index a7413cf..e9abaa8 100644 --- a/test/lib/liquid/file_system_test.rb +++ b/test/lib/liquid/file_system_test.rb @@ -5,7 +5,7 @@ class FileSystemTest < Test::Unit::TestCase def test_default assert_raise(FileSystemError) do - BlankFileSystem.new.read_template_file({'dummy'=>'smarty'}, "dummy") + BlankFileSystem.new.read_template_file("dummy", {'dummy'=>'smarty'}) end end diff --git a/test/lib/liquid/include_tag_test.rb b/test/lib/liquid/include_tag_test.rb index 814322c..101dd7b 100644 --- a/test/lib/liquid/include_tag_test.rb +++ b/test/lib/liquid/include_tag_test.rb @@ -1,8 +1,7 @@ require 'test_helper' class TestFileSystem - def read_template_file(context, template_name) - template_path = context[template_name] + def read_template_file(template_path, context) case template_path when "product" "Product: {{ product.title }} " @@ -35,8 +34,7 @@ class TestFileSystem end class OtherFileSystem - def read_template_file(context, template_name) - template_path = context[template_name] + def read_template_file(template_path, context) 'from OtherFileSystem' end end @@ -106,7 +104,7 @@ class IncludeTagTest < Test::Unit::TestCase def test_recursively_included_template_does_not_produce_endless_loop infinite_file_system = Class.new do - def read_template_file(context, template_name) + def read_template_file(template_path, context) "-{% include 'loop' %}" end end @@ -119,6 +117,18 @@ class IncludeTagTest < Test::Unit::TestCase 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 assert_equal "Test123", Template.parse("{% include template %}").render("template" => 'Test123')