Skip to main content

Lektor plugin to add tags.

Project description

Lektor Tags Plugin

PyPI version Code style: black

Introduction

This plugin implements tagging for your site. For each of your tags, it builds a page displaying a list of items that have that tag. This can be used for standard tag-based blog navigation. With this plugin you can give any number of tags to any blog posts, and a page will be created for each tag.

For example, if your site has blog posts in your content/blog directory tagged with coffee and tea:

name: First Post
---
tags:

coffee
tea

The lektor-tags plugin builds pages at these URLs:

  • /blog/tag/coffee/
  • /blog/tag/tea/

Each page can list all the posts with that tag.

Installation

Add lektor-tags to your project from command line:

lektor plugins add lektor-tags

See the Lektor documentation for more instructions on installing plugins.

Overview

Say you have a "blog-post" model like this:

[model]
name = Blog Post

[fields.tags]
type = strings

Make a blog-post.html template that includes:

{% if this.tags %}
  <ul>
    {% for t in this.tags -%}
      <li>
        <a href="{{ ('/blog@tag/' ~ t.lower())|url }}">
          All posts tagged {{ t }}
        </a>
      </li>
    {% endfor %}
  </ul>
{% endif %}

This expression in the template generates a source path for each of the blog post's tags:

'/blog@tag/' ~ t.lower()

Then if the tag is "my-tag", the expression renders a source path like:

/blog/tag/my-tag

A Lektor source path becomes an actual URL using the url filter. So the template generates URLs to tag pages like:

<a href="{{ ('/blog@tag/' ~ t.lower())|url }}"></a>

This uses the source path expression from before, but pipes it through url to generate an actual link from the blog post to a tag page.

Configuration

Set these options in configs/tags.ini:

parent

Required. The source path of the tag pages' parent page. For example:

parent = /blog

Then tag pages' source paths are like:

/blog/tag/my-tag

You can specify the root as the parent:

parent = /

items

A query for all items on the page for one tag. You can use the variables site and tag. The template's this variable has a parent attribute. The default query is:

items = this.parent.children.filter(F.tags.contains(tag))

You can sort and filter with any expression:

items = this.parent.children.filter(F.tags.contains(tag) and F.status == 'published').order_by('-pub_date')

If the parent page has a pagination query you may want to use it for tagged pages:

items = this.parent.pagination.items.filter(F.tags.contains(tag))

See the Lektor documentation for queries.

tags_field

The name of the field in your model that contains tags. Defaults to tags. The field should be of type strings. See the Lektor documentation for the strings type.

For example, if your model is like:

[fields.labels]
type = strings

Then add this to tags.ini:

tags_field = labels

template

The template for the page that lists all posts with a certain tag. The template's this variable has attributes tag and items. An example template:

<h1>Tag: {{ this.tag }}</h1>
<h1>Items:</h1>
<ul>
  {% for i in this.items %}
    <li><a href="{{ i|url }}">{{ i._id }}</a></li>
  {% else %}
    <li><em>No items.</em></li>
  {% endfor %}
</ul>

Save a file like this to your project's templates/tags.html. If you name the file something different, like label.html, add this line to tags.ini:

template = label.html

The plugin provides a default template if you don't specify one.

url_path

An expression for the location of each tag page. You can use the variables site and tag. The this variable is a page with attributes parent and items. The default expression is:

url_path = {{ this.parent.url_path }}tag/{{ tag }}

This expression generates URLs like /blog/tag/coffee.

ignore_missing

Default false. To set true, add this line to tags.ini:

ignore_missing = true

This allows URLs to missing tag pages to be silently replaced with "". The example use case is if your blog-post.html template includes a statement like:

{% for t in this.tags -%}
  <a href="{{ ('/blog@tag/' ~ t.lower())|url }}">{{ t }}</a>
{% endfor %}

If a blog post draft is not discoverable, and it has any new tags used by no published blog posts, then those tag pages do not yet exist. Turn on ignore_missing to allow such drafts to be built. The tag-page URL path will be the empty string "", until the draft is published and the tag page is created.

tags

Advanced configuration. An expression for the set of tags. The default expression is:

tags = parent.children.distinct("tags")

If you set tags_field to a different field name than "tags", the default expression uses your custom field name. For example if you have this line in tags.ini:

tags_field = labels

Then the default value of tags is:

tags = parent.children.distinct("labels")

You can use any template expression. For example, if your items have a "published" boolean field, you can select tags of published items:

tags = parent.children.filter(F.published).distinct("tags")

Or even list your tags manually:

tags = ["tag1", "tag2"]

See the Lektor documentation for queries.

Tags are always deduplicated, and alphabetically ordered.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lektor-tags-0.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

lektor_tags-0.3-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file lektor-tags-0.3.tar.gz.

File metadata

  • Download URL: lektor-tags-0.3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for lektor-tags-0.3.tar.gz
Algorithm Hash digest
SHA256 283e1687914bdd7d4e2766678f81bfea7d6224403cb17bce9f3c66800904d949
MD5 75f26a97af0048b96a3942156db0998e
BLAKE2b-256 c343bc69845f9d15555750c1fa52135283223086d2ee761443b3e0999ce7c468

See more details on using hashes here.

File details

Details for the file lektor_tags-0.3-py3-none-any.whl.

File metadata

  • Download URL: lektor_tags-0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.0

File hashes

Hashes for lektor_tags-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e995ec987ec51649aa61d8c956552a7feae797f5b8df092fbb92e213817121d7
MD5 9741c65019c6562a179026515beb8ba2
BLAKE2b-256 67ae02fef1b85334d310512533601be1fbe5e21955e5a3f60f19dff4967df1b8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page