Views

Now, you know about transforming your content, you’ll learn how to aggregate your content. Acrylamid ships a few views but you are not limited to them. You can easily write your own view without forking the project (more on this topic later). Currently, Acrylamid ships the following:

  • article overview, an aggregation of all posts grouped by year and month.
  • entry view, a single full-text post.
  • index view, pagination of your content.
  • tag view, per tag pagination.
  • feed view, offering RSS and Atom aggregation (also per tag).
  • static site search, self-explanatory.

All views have some properties in common such as path, filters and conditionals, you’ve to set in your Configuration. The idea of views is similar to routes in Django or Flask. You can set your URL setup to whatever you like, Acrylamid is not fixed to a single directory structure. For convenience, Acrylamid appends an index.html to a URL if it does end with a slash (as shown in the defaults).

All views share some options such as view type, conditionals or route in common, but can also offer individual parameters, you’ll find explained in the built-in views section.

path : string
The route/path of the view(s). Note that you can’t add the same route a second time, but you can add multiple views to a path.
view : string
Name of your wished view. Exact case does not matter that much.
views : list of strings
A list of views as described above.
filters : string or list of strings
A list of filters or a single filter name to apply to the view.
if : lambda/function
A condition to filter your content before they are passed to the view.

Here’s an example of how to use views:

VIEWS = {
    "/path/": {"view": "translation", "if": lambda e: e.lang == 'klingon'}
}

To see, what variables are available during templating, consult Templating.

Built-in Views

class acrylamid.views.archive.Archive

A view that lists all posts per year/month/day – usually found in WordPress blogs. Configuration syntax:

'/:year/': {'view': 'archive'},
'/:year/:month/': {'view': 'archive'},
'/:year/:month/:day/': {'view': 'archive'}

During templating you can access the current archive year/month/day via env.archive which basically holds the year, month and day, although the latter two may be None in case of a route without month or day. To determine the current archive name, you can use the following snippet:

{% set archivesname = env.archive.year
                    ~ (('/' ~ env.archive.month) if env.archive.month else '')
                    ~ (('/' ~ env.archive.day) if env.archive.day else '')  %}

Rendering a list of entries is the same like in other views:

{% for entry in env.entrylist %}
<a href="{{ entry.permalink }}">{{ entry.title | e }}</a>
{% endfor %}

To link to the archive pages, you can either link the entry date as there is at least one entry in that timerange: the entry itself. But you can also generate a complete archive listing as you may know from WordPress. This does only includes years, months and/or days where you have at least a single post.

{% for year in env.globals.entrylist | archivesfor %}
<h2>{{ year ~ ' (' ~ year | count ~ ')' }}</h2>
<ul>
    {% for month in year %}
    <li>
        <a href="{{ env.path ~ '/' ~ year ~ '/' ~ month ~ '/'  }}">{{ month.full }}</a>
        ({{  month | count }})
    </li>
    {% endfor %}
</ul>
{% endfor %}

This generates a listing that shows the amount of postings during the period. You can also iterate over each month to get to the days. Year, Month and Day objects always evaluate to a zero-padded date unit such as 2012 (year) or 01 (January). In addition, Month and Day objects have full and abbr attributes to access the fullname or abbreviation in your current location.

class acrylamid.views.articles.Articles

Generates an overview of all articles using layouts/articles.html as default jinja2 template (Example).

To enable Articles view, add:

'/articles/' : {
    'view': 'articles',
    'template': 'articles.html'  # default
}

to your Configuration where /articles/ is the default URL for this view.

We filter articles that are drafts and add them to the articles dictionary using (entry.year, entry.imonth) as key. During templating we sort all keys by value, hence we get a listing of years > months > entries.

Variables available during Templating:

  • articles containing the articles
  • num_entries count of articles
  • conf, env
class acrylamid.views.entry.Entry

Creates single full-length entry (Example).

To enable Entry view, add this to your Configuration:

'/:year/:slug/': {
    'view': 'entry',
    'template': 'main.html'  # default, includes entry.html
}

The entry view renders an post to a unique location and should be used as permalink URL. The url is user configurable, but may be overwritten by setting ENTRY_PERMALINK explicitly to a URL in your configuration.

This view takes no other arguments and uses main.html and entry.html as template.

class acrylamid.views.feeds.Feed

Atom and RSS feed generation. The feeds module provides several classes to generate feeds:

  • RSS – RSS feed for all entries
  • Atom – same for Atom
  • RSSPerTag – RSS feed for all entries for a given tag
  • AtomPerTag – same for Atom

All feed views have a num_entries argument that defaults to 25 and limits the list of posts to the 25 latest ones. In addition RSSPerTag and AtomPerTag expand :name to the current tag in your route.

Examples:

# per tag Atom feed
'/tag/:name/feed/': {'filters': ..., 'view': 'atompertag'}

# full Atom feed
'/atom/full/': {'filters': ..., 'view': 'atom', 'num_entries': 1000}
class acrylamid.views.index.Index

Creates nicely paged listing of your posts. First page renders to route (defaults to /) with a recent list of your (e.g. summarized) articles. Other pages enumerate to the variable pagination (/page/:num/ per default).

'/' : {
    'view': 'index',
    'template': 'main.html',
    'pagination': '/page/:num/',
    'items_per_page': 10
}
class acrylamid.views.entry.Page

Creates a static page

To enable Entry view, add this to your Configuration:

'/:year/:slug/': {
    'view': 'page',
    'template': 'main.html'  # default, includes entry.html
}

The page view renders an post to a unique location withouth any references to other blog entries. The url is user configurable, but may be overwritten by setting PAGE_PERMALINK explicitly to a URL in your configuration.

This view takes no other arguments and uses main.html and entry.html as template.

class acrylamid.views.tag.Tag

Same behaviour like Index except route that defaults to /tag/:name/ and pagination that defaults to /tag/:name/:num/ where :name is the current tag identifier.

To create a tag cloud head over to Configuration.

class acrylamid.views.entry.Translation

Creates translation of a single full-length entry. To enable the Translation view, add this to your Configuration:

'/:year/:slug/:lang/': {
    'view': 'translation',
    'template': 'main.html',  # default, includes entry.html
}

Translations are posts with the same identifier and a different lang attribute. An example:

The English article:

---
title: Foobar is not dead
identifier: foobar-is-not-dead
---

That's true, foobar is still alive!

And the French version:

---
title: Foobar n'est pas mort !
identifier: foobar-is-not-dead
lang: fr
---

Oui oui, foobar est toujours vivant !

If the blog language is "en" then the english article will be included into the default listing but the french version not. You can link to the translated versions via:

{% if 'translation' in env.views and env.translationsfor(entry) %}
<ul>
    {% for tr in env.translationsfor(entry) %}
        <li><strong>{{ tr.lang }}:</strong>
            <a href="{{ env.path ~ tr.permalink }}">{{ tr.title }}</a>
        </li>
    {% endfor %}
</ul>
{% endif %}
class acrylamid.views.sitemap.Sitemap

Create an XML-Sitemap where permalinks have the highest priority (1.0) and do never change and all other ressources have a changefreq of weekly.

'/sitemap.xml': {
    'view': 'Sitemap'
}

Custom Views

You can easily extend Acrylamid by writing custom views directly in your blog directory. Just add VIEWS_DIR += ['views/'] to your Configuration and write your own view.

Table Of Contents

Related Topics

This Page