tyrel.cloud


All posts tagged:


Lektor Blog Tags

Updated by Tyrel on 2021-12-03

Summary

Blog tags help categorize topics and function as a very basic type of search mechanism

How to add blog tags

Non Official Guide: https://www.getlektor.com/plugins/lektor-tags/

Quick Note: This is a bit more custom than other plugins. What follows is my implementation. Not for the faint of heart!

  1. Add Plugin lektor plugins add lektor-tags
  2. Create configs directory in main project base location
  3. Create tags.ini file in configs directory
  4. Put this in your tags.ini file
    parent = /blog
    tags_field = tags
    ignore_missing = true
    template = tags.html
    url_path = {{ this.parent.url_path }}tag/{{ tag }}
    items = this.parent.children.filter(F.tags.contains(tag))
    
  5. Add this to your blog-post.ini model in your models directory
    [fields.tags]
    type = strings
    
  6. Add this to your blog-post.html template in your templates directory

    <div class="tags">
    <h1>All posts tagged:</h1>
    <ul>
     {% for t in this.tags -%}
     <li>
       <a href="{{ ('/blog@tag/' ~ t.lower())|url }}">
         {{ t }}
       </a>
     </li>
     {% endfor %}
    </ul>
    </div>
    
  7. Add this to your blog.html template in your templates directory

    {% set tags = site.query('/blog').distinct('tags') %}
    {% if tags %}
    <div class="tags">
    <h1>By Tag:</h1>
    <ul>
     {% for tag in tags|sort %}
     <li>
       <a href="{{ ('/blog@tag/' ~ tag.lower())|url }}">{{ tag }}</a>
     </li>
     {% else %}
     <li><em>No items.</em></li>
     {% endfor %}
    </ul>
    </div>
    {% endif %}
    
  8. Create tags directory in your content directory

  9. Create contents.lr file in your tags directory
  10. Add this to the contents.lr file

    _model: tags
    ---
    title: Tags
    
  11. Add this to your tags.html template in your templates directory

     {% extends "layout.html" %}
     {% block title %}{{ this.title }}{% endblock %}
     {% block body %}
    
     <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>
    
     {% endblock %}
    

You have tags! Wasn't that fun!?

At this point you need to crack open a celebratory beverage and possibly even pull out the peace pipe...

Feel like I missed something? Let me know in the comments!