Skip to content

Commit 1ac5dd9

Browse files
committed
Add support for type-specific layouts
1 parent 84f7766 commit 1ac5dd9

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

lib/jekyll-archives/archive.rb

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ class Archive
2525
# type - The type of archive. Can be one of "year", "month", "day", "category", or "tag"
2626
# posts - The array of posts that belong in this archive.
2727
def initialize(site, title, type, posts)
28-
@site = site
29-
@posts = posts
30-
@type = type
31-
@title = title
28+
@site = site
29+
@posts = posts
30+
@type = type
31+
@title = title
32+
@config = site.config['jekyll-archives']
3233

3334
# Generate slug if tag or category (taken from jekyll/jekyll/features/support/env.rb)
3435
if title.is_a? String
@@ -41,7 +42,7 @@ def initialize(site, title, type, posts)
4142
@name = File.basename(relative_path, @ext)
4243

4344
@data = {
44-
"layout" => site.config['jekyll-archives']['layout']
45+
"layout" => layout
4546
}
4647
@content = ""
4748
end
@@ -50,7 +51,18 @@ def initialize(site, title, type, posts)
5051
#
5152
# Returns the template String.
5253
def template
53-
site.config['jekyll-archives']['permalinks'][type]
54+
@config['permalinks'][type]
55+
end
56+
57+
# The layout to use for rendering
58+
#
59+
# Returns the layout as a String
60+
def layout
61+
if @config['layouts'] && @config['layouts'][type]
62+
@config['layouts'][type]
63+
else
64+
@config['layout']
65+
end
5466
end
5567

5668
# Returns a hash of URL placeholder names (as symbols) mapping to the
@@ -136,7 +148,7 @@ def relative_path
136148

137149
# Returns the object as a debug String.
138150
def inspect
139-
"#<Jekyll:Archive @type=#{@type.to_s} @title=#{@title}>"
151+
"#<Jekyll:Archive @type=#{@type.to_s} @title=#{@title} @data=#{@data.inspect}>"
140152
end
141153

142154
# Returns the Boolean of whether this Page is HTML or not.

test/test_jekyll_archives.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ class TestJekyllArchives < Minitest::Test
5656
"enabled" => true
5757
}
5858
})
59-
@site.read
60-
@archives = Jekyll::Archives.new(@site.config)
59+
@site.process
6160
end
6261

6362
should "use custom layout" do
@@ -66,6 +65,26 @@ class TestJekyllArchives < Minitest::Test
6665
end
6766
end
6867

68+
context "the jekyll-archives plugin with type-specific layout" do
69+
setup do
70+
@site = fixture_site({
71+
"jekyll-archives" => {
72+
"enabled" => true,
73+
"layouts" => {
74+
"year" => "archive-too"
75+
}
76+
}
77+
})
78+
@site.process
79+
end
80+
81+
should "use custom layout for specific type only" do
82+
assert_equal "Test too", read_file("/2014/index.html")
83+
assert_equal "Test too", read_file("/2013/index.html")
84+
assert_equal "Test", read_file("/tag/test-tag/index.html")
85+
end
86+
end
87+
6988
context "the jekyll-archives plugin with custom permalinks" do
7089
setup do
7190
@site = fixture_site({

0 commit comments

Comments
 (0)