2011-01-20 01:04:54 +01:00
.. _theming-pelican:
2010-12-05 19:15:02 +00:00
How to create themes for pelican
################################
2011-06-04 15:35:44 +02:00
Pelican uses the great `jinja2 <http://jinja.pocoo.org> `_ templating engine to
2011-01-05 14:04:11 +01:00
generate it's HTML output. The jinja2 syntax is really simple. If you want to
create your own theme, feel free to take inspiration from the "simple" theme,
which is available `here
<https://github.com/ametaireau/pelican/tree/master/pelican/themes/simple/templates> `_
2010-12-05 19:15:02 +00:00
Structure
=========
To make your own theme, you must follow the following structure::
├── static
│ ├── css
│ └── images
└── templates
2010-12-15 18:13:59 +00:00
├── archives.html // to display archives
├── article.html // processed for each article
2011-07-16 00:44:49 +02:00
├── author.html // processed for each author
├── authors.html // must list all the authors
2010-12-15 18:13:59 +00:00
├── categories.html // must list all the categories
├── category.html // processed for each category
├── index.html // the index. List all the articles
├── page.html // processed for each page
├── tag.html // processed for each tag
└── tags.html // must list all the tags. Can be a tag cloud.
2010-12-05 19:15:02 +00:00
* `static` contains all the static content. It will be copied on the output
`theme/static` folder then. I've put the css and image folders, but they are
just examples. Put what you need here.
* `templates` contains all the templates that will be used to generate the content.
2011-04-26 02:49:00 +02:00
I've just put the mandatory templates here, you can define your own if it helps
2010-12-05 19:15:02 +00:00
you to organize yourself while doing the theme.
Templates and variables
=======================
It's using a simple syntax, that you can embbed into your html pages.
2011-04-26 02:49:00 +02:00
This document describes which templates should exist on a theme, and which
2010-12-05 19:15:02 +00:00
variables will be passed to each template, while generating it.
2011-04-26 02:49:00 +02:00
All templates will receive the variables defined in your settings file, if they
2010-12-05 19:15:02 +00:00
are in caps. You can access them directly.
Common variables
----------------
2010-12-15 18:08:25 +00:00
All of those settings will be given to all templates.
2010-12-05 19:15:02 +00:00
============= ===================================================
Variable Description
============= ===================================================
2011-06-04 15:35:44 +02:00
articles That's the list of articles, ordered desc. by date
2010-12-05 19:15:02 +00:00
all the elements are `Article` objects, so you can
access their properties (e.g. title, summary, author
2011-06-04 15:35:44 +02:00
etc.).
2010-12-05 19:15:02 +00:00
dates The same list of article, but ordered by date,
2011-06-04 15:35:44 +02:00
ascending.
2010-12-05 19:15:02 +00:00
tags A dict containing each tags (keys), and the list of
relative articles.
categories A dict containing each category (keys), and the
list of relative articles.
2011-06-04 15:35:44 +02:00
pages The list of pages.
2010-12-05 19:15:02 +00:00
============= ===================================================
2011-02-18 11:21:33 +01:00
index.html
----------
Home page of your blog, will finally remain at output/index.html.
If pagination is active, next pages will remain at output/index`n` .html.
=================== ===================================================
Variable Description
=================== ===================================================
2011-06-04 15:35:44 +02:00
articles_paginator A paginator object of article list.
articles_page The current page of articles.
2011-02-18 11:21:33 +01:00
dates_paginator A paginator object of article list, ordered by date,
2011-06-04 15:35:44 +02:00
ascending.
2011-02-18 11:21:33 +01:00
dates_page The current page of articles, ordered by date,
2011-06-04 15:35:44 +02:00
ascending.
2011-02-18 12:33:11 +00:00
page_name 'index'. Useful for pagination links.
2011-02-18 11:21:33 +01:00
=================== ===================================================
2011-07-16 00:44:49 +02:00
author.html
-------------
This template will be processed for each of the existing authors, and will
finally remain at output/author/`author_name` .html.
If pagination is active, next pages will remain at
output/author/`author_name` `n` .html.
=================== ===================================================
Variable Description
=================== ===================================================
author The name of the author being processed.
articles Articles of this author.
dates Articles of this author, but ordered by date,
ascending.
articles_paginator A paginator object of article list.
articles_page The current page of articles.
dates_paginator A paginator object of article list, ordered by date,
ascending.
dates_page The current page of articles, ordered by date,
ascending.
page_name 'author/`author_name` '. Useful for pagination
links.
=================== ===================================================
2010-12-05 19:15:02 +00:00
category.html
-------------
2010-12-15 18:08:25 +00:00
This template will be processed for each of the existing categories, and will
finally remain at output/category/`category_name` .html.
2011-02-18 11:21:33 +01:00
If pagination is active, next pages will remain at
output/category/`category_name` `n` .html.
=================== ===================================================
Variable Description
=================== ===================================================
2011-06-04 15:35:44 +02:00
category The name of the category being processed.
articles Articles of this category.
2011-02-18 11:21:33 +01:00
dates Articles of this category, but ordered by date,
2011-06-04 15:35:44 +02:00
ascending.
articles_paginator A paginator object of article list.
articles_page The current page of articles.
2011-02-18 11:21:33 +01:00
dates_paginator A paginator object of article list, ordered by date,
2011-06-04 15:35:44 +02:00
ascending.
2011-02-18 11:21:33 +01:00
dates_page The current page of articles, ordered by date,
2011-06-04 15:35:44 +02:00
ascending.
2011-02-18 12:33:11 +00:00
page_name 'category/`category_name` '. Useful for pagination
2011-02-18 11:21:33 +01:00
links.
=================== ===================================================
2010-12-05 19:15:02 +00:00
article.html
-------------
2011-04-26 02:49:00 +02:00
This template will be processed for each article. .html files will be output
2010-12-15 18:08:25 +00:00
in output/`article_name` .html. Here are the specific variables it gets.
2010-12-05 19:15:02 +00:00
============= ===================================================
Variable Description
============= ===================================================
2011-06-04 15:35:44 +02:00
article The article object to be displayed.
category The name of the category of the current article.
============= ===================================================
page.html
---------
For each page, this template will be processed. It will create .html files in
output/`page_name` .html.
============= ===================================================
Variable Description
============= ===================================================
page The page object to be displayed. You can access to
its title, slug and content.
2010-12-05 19:15:02 +00:00
============= ===================================================
tag.html
--------
2010-12-15 18:08:25 +00:00
For each tag, this template will be processed. It will create .html files in
2011-06-04 15:35:44 +02:00
output/tag/`tag_name` .html.
2011-02-18 11:21:33 +01:00
If pagination is active, next pages will remain at
output/tag/`tag_name` `n` .html.
=================== ===================================================
Variable Description
=================== ===================================================
2011-06-04 15:35:44 +02:00
tag The name of the tag being processed.
articles Articles related to this tag.
2011-02-18 11:21:33 +01:00
dates Articles related to this tag, but ordered by date,
2011-06-04 15:35:44 +02:00
ascending.
articles_paginator A paginator object of article list.
articles_page The current page of articles.
2011-02-18 11:21:33 +01:00
dates_paginator A paginator object of article list, ordered by date,
2011-06-04 15:35:44 +02:00
ascending.
2011-02-18 11:21:33 +01:00
dates_page The current page of articles, ordered by date,
2011-06-04 15:35:44 +02:00
ascending.
2011-02-18 12:33:11 +00:00
page_name 'tag/`tag_name` '. Useful for pagination links.
2011-02-18 11:21:33 +01:00
=================== ===================================================
2011-02-22 11:35:06 +01:00
2011-07-23 14:53:55 +02:00
Inheritance
===========
2011-07-23 16:06:58 +02:00
Since version 3, pelican supports inheritance from the `` simple `` theme, so you can reuse the templates of the `` simple `` theme in your own themes:
2011-07-23 14:53:55 +02:00
2011-07-23 16:06:58 +02:00
If one of the mandatory files in the `` templates/ `` directory of your theme is missing, it will be replaced by the matching template from the `` simple `` theme, so if the HTML structure of a template of the `` simple `` theme is right for you, you don't have to rewrite it from scratch.
2011-07-23 14:53:55 +02:00
2011-07-23 16:06:58 +02:00
You can also extend templates of the `` simple `` themes in your own themes by using the `` {% extends %} `` directive as in the following example:
2011-07-23 14:53:55 +02:00
.. code-block :: html+jinja
{% extends "!simple/index.html" %} <!-- extends the `` index.html `` template of the `` simple `` theme -->
{% extends "index.html" %} <!-- "regular" extending -->
Example
-------
2011-07-23 16:06:58 +02:00
With this system, it is possible to create a theme with just two files.
2011-07-23 14:53:55 +02:00
base.html
"""""""""
The first file is the `` templates/base.html `` template:
.. code-block :: html+jinja
{% extends "!simple/base.html" %}
{% block head %}
{{ super() }}
<link rel="stylesheet" type="text/css" href="{{ SITEURL }}/theme/css/style.css" />
{% endblock %}
2011-07-23 16:06:58 +02:00
1. On the first line, we extend the `` base.html `` template of the `` simple `` theme, so we don't have to rewrite the entire file.
2. On the third line, we open the `` head `` block which has already been defined in the `` simple `` theme
2011-07-23 14:53:55 +02:00
3. On the fourth line, the function `` super() `` keeps the content previously inserted in the `` head `` block.
4. On the fifth line, we append a stylesheet to the page
5. On the last line, we close the `` head `` block.
2011-07-23 16:06:58 +02:00
This file will be extended by all the other templates, so the stylesheet will be linked from all pages.
2011-07-23 14:53:55 +02:00
style.css
"""""""""
The second file is the `` static/css/style.css `` CSS stylesheet:
.. code-block :: css
body {
font-family : monospace ;
font-size : 100% ;
background-color : white ;
color : #111 ;
width : 80% ;
min-width : 400px ;
min-height : 200px ;
padding : 1em ;
margin : 5% 10% ;
border : thin solid gray ;
border-radius : 5px ;
display : block ;
}
a:link { color : blue ; text-decoration : none ; }
a:hover { color : blue ; text-decoration : underline ; }
a:visited { color : blue ; }
h1 a { color : inherit !important }
h2 a { color : inherit !important }
h3 a { color : inherit !important }
h4 a { color : inherit !important }
h5 a { color : inherit !important }
h6 a { color : inherit !important }
pre {
margin : 2em 1em 2em 4em ;
}
#menu li {
display : inline ;
}
#post-list {
margin-bottom : 1em ;
margin-top : 1em ;
}
Download
""""""""
You can download this example theme :download: `here <_static/theme-basic.zip>` .