Skip to content

Commit 8633bc1

Browse files
committed
Merge pull request #26 from jekyll/page-dot-date
Move Date object from `page.title` to `page.date`
2 parents aad5d0b + e35d6dd commit 8633bc1

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

docs/layouts.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# Layouts
22

3-
Archives layouts are special layouts that specify how an archive page is displayed. Special attributes are available to these layouts to represent information about the specific layout being generated. These layouts are otherwise identical to regular Jekyll layouts.
3+
Archives layouts are special layouts that specify how an archive page is displayed. Special attributes are available to these layouts to represent information about the specific layout being generated. These layouts are otherwise identical to regular Jekyll layouts. To handle the variety of cases presented through the attributes, we recommend that you use [type-specific layouts](./configuration.md#type-specific-layouts).
44

55
### Layout attributes
66
#### Title (`page.title`)
7-
The `page.title` attribute contains information regarding the name or header of the archive. For tag and category archives, this is simply the name of the tag/category. For date-based archives (year, month, and day), this attribute contains a Date object that can be used to present the date header of the archive in a suitable format. For year archives, the month and day components of the Date object passed to Liquid should be neglected; similarly, for month archives, the day component should be neglected. To handle the variety of cases presented, we recommend that you use [type-specific layouts](./configuration.md#type-specific-layouts). We recommend using the [`date` filter](http://docs.shopify.com/themes/liquid-documentation/filters/additional-filters#date) in Liquid to process the Date objects.
7+
The `page.title` attribute contains information regarding the name of the archive *if and only if* the archive is a tag or category archive. In this case, the attribute simply contains the name of the tag/category. For date-based archives (year, month, and day), this attribute is `nil`.
8+
9+
#### Date (`page.date`)
10+
In the case of a date archive, this attribute contains a Date object that can be used to present the date header of the archive in a suitable format. For year archives, the month and day components of the Date object passed to Liquid should be neglected; similarly, for month archives, the day component should be neglected. We recommend using the [`date` filter](http://docs.shopify.com/themes/liquid-documentation/filters/additional-filters#date) in Liquid to process the Date objects. For tag and category archives, this field is `nil`.
811

912
#### Posts (`page.posts`)
1013
The `page.posts` attribute contains an array of Post objects matching the archive criteria. You can iterate over this array just like any other Post array in Jekyll.

lib/jekyll-archives/archive.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Archive
1313
posts
1414
type
1515
title
16+
date
1617
name
1718
path
1819
url
@@ -116,14 +117,21 @@ def to_liquid(attrs = nil)
116117

117118
# Produce a title object suitable for Liquid based on type of archive.
118119
#
119-
# Returns the title as a Date (for date-based archives) or a
120-
# String (for tag and category archives)
120+
# Returns a String (for tag and category archives) and nil for
121+
# date-based archives.
121122
def title
123+
if @title.is_a? String
124+
@title
125+
end
126+
end
127+
128+
# Produce a date object if a date-based archive
129+
#
130+
# Returns a Date.
131+
def date
122132
if @title.is_a? Hash
123133
args = @title.values.map { |s| s.to_i }
124134
Date.new(*args)
125-
else
126-
@title
127135
end
128136
end
129137

test/test_jekyll_archives.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,43 @@ class TestJekyllArchives < Minitest::Test
158158
assert !archive_exists?(@site, "category/plugins/index.html")
159159
end
160160
end
161+
162+
context "the jekyll-archives plugin" do
163+
setup do
164+
@site = fixture_site({
165+
"jekyll-archives" => {
166+
"enabled" => true
167+
}
168+
})
169+
@site.process
170+
@archives = @site.config["archives"]
171+
@tag_archive = @archives.detect {|a| a.type == "tag"}
172+
@category_archive = @archives.detect {|a| a.type == "category"}
173+
@year_archive = @archives.detect {|a| a.type == "year"}
174+
@month_archive = @archives.detect {|a| a.type == "month"}
175+
@day_archive = @archives.detect {|a| a.type == "day"}
176+
end
177+
178+
should "populate the title field in case of category or tag" do
179+
assert @tag_archive.title.is_a? String
180+
assert @category_archive.title.is_a? String
181+
end
182+
183+
should "use nil for the title field in case of dates" do
184+
assert @year_archive.title.nil?
185+
assert @month_archive.title.nil?
186+
assert @day_archive.title.nil?
187+
end
188+
189+
should "use nil for the date field in case of category or tag" do
190+
assert @tag_archive.date.nil?
191+
assert @category_archive.date.nil?
192+
end
193+
194+
should "populate the date field with a Date in case of dates" do
195+
assert @year_archive.date.is_a? Date
196+
assert @month_archive.date.is_a? Date
197+
assert @day_archive.date.is_a? Date
198+
end
199+
end
161200
end

0 commit comments

Comments
 (0)