diff --git a/History.md b/History.md index 306e43c..4b114c1 100644 --- a/History.md +++ b/History.md @@ -3,6 +3,7 @@ ## 3.0.0 / not yet released / branch "master" * ... +* Allow specifying custom patterns for template filenames, see #284 [Andrei Gladkyi, agladkyi] * Allow drops to optimize loading a slice of elements, see #282 [Tom Burns, boourns] * Support for passing variables to snippets in subdirs, see #271 [Joost Hietbrink, joost] * Add a class cache to avoid runtime extend calls, see #249 [James Tucker, raggi] diff --git a/lib/liquid/file_system.rb b/lib/liquid/file_system.rb index c7eeddc..b3efef6 100644 --- a/lib/liquid/file_system.rb +++ b/lib/liquid/file_system.rb @@ -31,11 +31,22 @@ module Liquid # file_system.full_path("mypartial") # => "/some/path/_mypartial.liquid" # file_system.full_path("dir/mypartial") # => "/some/path/dir/_mypartial.liquid" # + # Optionally in the second argument you can specify a custom pattern for template filenames. + # The Kernel::sprintf format specification is used. + # Default pattern is "_%s.liquid". + # + # Example: + # + # file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html") + # + # file_system.full_path("index") # => "/some/path/index.html" + # class LocalFileSystem attr_accessor :root - def initialize(root) + def initialize(root, pattern = "_%s.liquid") @root = root + @pattern = pattern end def read_template_file(template_path, context) @@ -49,9 +60,9 @@ module Liquid raise FileSystemError, "Illegal template name '#{template_path}'" unless template_path =~ /^[^.\/][a-zA-Z0-9_\/]+$/ full_path = if template_path.include?('/') - File.join(root, File.dirname(template_path), "_#{File.basename(template_path)}.liquid") + File.join(root, File.dirname(template_path), @pattern % File.basename(template_path)) else - File.join(root, "_#{template_path}.liquid") + File.join(root, @pattern % template_path) end raise FileSystemError, "Illegal template path '#{File.expand_path(full_path)}'" unless File.expand_path(full_path) =~ /^#{File.expand_path(root)}/ diff --git a/test/liquid/file_system_test.rb b/test/liquid/file_system_test.rb index e9abaa8..4b2a2f1 100644 --- a/test/liquid/file_system_test.rb +++ b/test/liquid/file_system_test.rb @@ -26,4 +26,10 @@ class FileSystemTest < Test::Unit::TestCase file_system.full_path("/etc/passwd") end end + + def test_custom_template_filename_patterns + file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html") + assert_equal "/some/path/mypartial.html" , file_system.full_path("mypartial") + assert_equal "/some/path/dir/mypartial.html", file_system.full_path("dir/mypartial") + end end # FileSystemTest