Skip to content

Commit 3bf6c2c

Browse files
committed
feat: Make the date format customisable
I am not entirely certain that using a partial is the right solution here, as a site-level configuration variable feels more appropriate. However, I was not able to figure out how to define a "default" format without using an `if-else` block, so I chose to implement this using partials. The if-else block would reduce the readability of the existing `single.html` template, as it may look something like this: ``` {{ if .Site.Params.dateFormat }}{{ .Date.Format .Site.Params.dateFormat }}{{ else }} {{.Date.Format "2006/01/02" }}{{ end }} ```
1 parent c871e56 commit 3bf6c2c

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

exampleSite/content/about.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ Here `{Year}` means the year in which the site is built (usually the current yea
5151
5252
## Custom layouts
5353
54-
There are two layout files under `layouts/_partials/` that you may want to override: `head_custom.html` and `foot_custom.html`. This is how you inject arbitrary HTML code to the head and foot areas. For example, this site has a file `layouts/_partials/foot_custom.html` to support LaTeX math via KaTeX and center images automatically:
54+
There are three layout files under `layouts/_partials/` that you may want to override:
55+
`head_custom.html`, `foot_custom.html`, and `date.html`.
56+
57+
This is how you inject arbitrary HTML code to the head and foot areas. For example, this site has a file `layouts/_partials/foot_custom.html` to support LaTeX math via KaTeX and center images automatically:
5558
5659
```html
5760
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/katex/dist/katex.min.css">
@@ -64,6 +67,13 @@ You can certainly enable highlight.js for syntax highlighting by yourself throug
6467

6568
If you do not like the default fonts (e.g., `Palatino`), you may provide your own `static/css/fonts.css` under the root directory of your website to override the `fonts.css` in the theme.
6669

70+
You can also change the format in which dates are displayed, by over-riding the `date.html` file. For instance, instead of `2006/01/02` (default date format), if you would like to display the date as `2006-01-02`, then the content of the `date.html` file should be:
71+
72+
``` html
73+
{{ .Date.Format "2006-01-02" }}
74+
75+
```
76+
6777
## Other features
6878

6979
I could have added more features to this theme, but I decided not to, since I have no intention to make this theme feature-rich. However, I will teach you how. I have prepared several examples via pull requests at https://github.com/yihui/hugo-xmin/pulls, so that you can see the implementations of these features when you check out the diffs in the pull requests. For example, you can:

layouts/_partials/date.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ .Date.Format "2006/01/02" }}

layouts/list.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h1>{{ .Title | markdownify }}</h1>
1111
{{ if .IsHome }}{{ $pages = .Site.RegularPages }}{{ end }}
1212
{{ range (where $pages "Section" "!=" "") }}
1313
<li>
14-
<span class="date">{{ .Date.Format "2006/01/02" }}</span>
14+
<span class="date">{{ partial "date.html" . }}</span>
1515
<a href="{{ .RelPermalink }}">{{ .Title | markdownify }}</a>
1616
</li>
1717
{{ end }}

layouts/single.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<div class="article-meta">
33
<h1><span class="title">{{ .Title | markdownify }}</span></h1>
44
{{ with .Params.author }}<h2 class="author">{{ . }}</h2>{{ end }}
5-
{{ if (gt .Params.date 0) }}<h2 class="date">{{ .Date.Format "2006/01/02" }}</h2>{{ end }}
5+
{{ if (gt .Params.date 0) }}<h2 class="date">{{ partial "date.html" . }}</h2>{{ end }}
66
</div>
77

88
<main>

0 commit comments

Comments
 (0)