Skip to content

Commit be7bef4

Browse files
committed
Merge pull request Shopify#284 from agladkyi/custom-patterns-for-template-filenames
Custom patterns for template filenames
2 parents 0ec6989 + 0ae42bb commit be7bef4

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 3.0.0 / not yet released / branch "master"
44

55
* ...
6+
* Allow specifying custom patterns for template filenames, see #284 [Andrei Gladkyi, agladkyi]
67
* Allow drops to optimize loading a slice of elements, see #282 [Tom Burns, boourns]
78
* Support for passing variables to snippets in subdirs, see #271 [Joost Hietbrink, joost]
89
* Add a class cache to avoid runtime extend calls, see #249 [James Tucker, raggi]

lib/liquid/file_system.rb

+14-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,22 @@ def read_template_file(template_path, context)
3131
# file_system.full_path("mypartial") # => "/some/path/_mypartial.liquid"
3232
# file_system.full_path("dir/mypartial") # => "/some/path/dir/_mypartial.liquid"
3333
#
34+
# Optionally in the second argument you can specify a custom pattern for template filenames.
35+
# The Kernel::sprintf format specification is used.
36+
# Default pattern is "_%s.liquid".
37+
#
38+
# Example:
39+
#
40+
# file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html")
41+
#
42+
# file_system.full_path("index") # => "/some/path/index.html"
43+
#
3444
class LocalFileSystem
3545
attr_accessor :root
3646

37-
def initialize(root)
47+
def initialize(root, pattern = "_%s.liquid")
3848
@root = root
49+
@pattern = pattern
3950
end
4051

4152
def read_template_file(template_path, context)
@@ -49,9 +60,9 @@ def full_path(template_path)
4960
raise FileSystemError, "Illegal template name '#{template_path}'" unless template_path =~ /^[^.\/][a-zA-Z0-9_\/]+$/
5061

5162
full_path = if template_path.include?('/')
52-
File.join(root, File.dirname(template_path), "_#{File.basename(template_path)}.liquid")
63+
File.join(root, File.dirname(template_path), @pattern % File.basename(template_path))
5364
else
54-
File.join(root, "_#{template_path}.liquid")
65+
File.join(root, @pattern % template_path)
5566
end
5667

5768
raise FileSystemError, "Illegal template path '#{File.expand_path(full_path)}'" unless File.expand_path(full_path) =~ /^#{File.expand_path(root)}/

test/liquid/file_system_test.rb

+6
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,10 @@ def test_local
2626
file_system.full_path("/etc/passwd")
2727
end
2828
end
29+
30+
def test_custom_template_filename_patterns
31+
file_system = Liquid::LocalFileSystem.new("/some/path", "%s.html")
32+
assert_equal "/some/path/mypartial.html" , file_system.full_path("mypartial")
33+
assert_equal "/some/path/dir/mypartial.html", file_system.full_path("dir/mypartial")
34+
end
2935
end # FileSystemTest

0 commit comments

Comments
 (0)