2015-11-12
The create
command adds sample templates to the templates
directory. Of course, you can change them or create your own from scratch.
LitePub uses the Go html/template package to define the templates.
Design changes require no knowledge of Go templates. However changes that affect what data is displayed will require it less or more (depending on the change).
There are four required files in the templates
directory:
templates/ # the templates and accompanying files (html, css, js, png, etc.)
layout.tmpl
index.tmpl
post.tmpl
tag.tmpl
layout.tmpl
defines the common layout for the home page (index.tmpl
), post pages (post.tmpl
) and tag pages (tag.tmpl
)index.tmpl
is used when generating the home page (index.html
)post.tmpl
is used when generating post pagestag.tmpl
is used when generating tag pagesBesides the four files there can be any number of html
, css
, js
, png
, etc. files that are used by the .tmpl
files.
If you're not familiar with Go templates, some things in the next sections can be unclear.
Templates have access to data they are meant to display. There are two types of data: Post
s and Tag
s.
A Post
has the following properties:
Title
- the post titleContent
- the content of the post as Markdown textWritten
- the post's dateTags
- an array of tags the post is tagged with (can be empty)Draft
- true
if the post is a draftTo get a post's page URL in a template use the
slug
function (described below) like this:<a href="/{{.Title | slug}}.html">A Post</a>
.
A Tag
has the following properties:
Name
- the tag namePosts
- an array of Post
s that are tagged with the tag sorted by Written
in descending orderTo get a tag's page URL in a template use the
slug
function (described below) like this:<a href="/tags/{{.Name | slug}}.html">A Tag</a>
.
The index.tmpl
template has access to an array of Post
s sorted by Written
in descending order. The post.tmpl
template has access to the Post
it displays. The tag.tmpl
template has access to the Tag
it displays.
The index.tmpl
, post.tmpl
and tag.tmpl
templates have access to the following functions:
Converts a Markdown string to a raw HTML, for example {{.Content | html}}
.
Extracts the first paragraph of a Markdown string that isn't a header (doesn't start with a #
), for example {{.Content | summary | html}}
.
Returns true
if an integer is even, for example {{if even $i}}<divclass="row">{{end}}
.
Increments an integer by one, for example {{if or (eq (inc $i) $l) (not (even $i))}}</div>{{end}}
.
Slugifies a string, for example <a href="/{{.Title | slug}}.html">A Post</a>
.
The available functions represent my needs when converting my handmade blog to a generated one.
Next: Getting Help
Hi, i'm and this is my place.
This is my picture:
I'm a web developer. I like Ruby the most but i'm fine with other technology also. After all, it's just technology.
I work at topmonks.com. I also have my own tiny company Imagine Anything.
One of my side projects is DameData.cz. It's a web app which helps you to easily connect your business with accounting(czech market only right now). Check it out:
Have a good one
Follow