{% extends "base.html" %} {# Category Browser Template Displays all categories with post counts and recent posts. Perfect for documentation sites to browse by category/type. Uses query indexes for O(1) category lookups. URL Pattern: /categories/ Usage: Create content/categories/_index.md: --- title: "Browse by Category" template: category-browser.html description: "Explore documentation organized by category" --- Optional metadata: - sections: ["docs", "blog"] (only show categories from these sections) - show_recent: 3 (number of recent posts to show per category) - layout: "grid" or "list" (default: "grid") Context variables: - page: Current page - site.indexes.category: Category index #} {% from 'partials/navigation-components.html' import breadcrumbs %} {% from 'partials/content-components.html' import tag_list %} {% set section_filters = page.metadata.get('sections', []) %} {% set show_recent = page.metadata.get('show_recent', 3) %} {% set layout = page.metadata.get('layout', 'grid') %} {% block content %} {{ breadcrumbs(page) }}

{{ page.title | default('Browse by Category') }}

{% if page.metadata.get('description') %}

{{ page.metadata.get('description') }}

{% endif %}
{# Main content from markdown #} {% if content %}
{{ content | safe }}
{% endif %} {# Get all categories from index #} {% set all_categories = site.indexes.category.keys() | sort %} {% if all_categories %} {# Summary stats #}
{{ all_categories | length }} categories {% set total_posts = namespace(count=0) %} {% for category in all_categories %} {% set category_posts = site.indexes.category.get(category) | resolve_pages %} {% if section_filters %} {% set filtered_posts = [] %} {% for post in category_posts %} {% if post._section.name in section_filters %} {% do filtered_posts.append(post) %} {% endif %} {% endfor %} {% set category_posts = filtered_posts %} {% endif %} {% set total_posts.count = total_posts.count + (category_posts | length) %} {% endfor %} {{ total_posts.count }} total pages
{# Layout toggle #}
{# Category Grid/List #}
{% for category in all_categories %} {# O(1) lookup for category pages #} {% set category_posts = site.indexes.category.get(category) | resolve_pages %} {# Filter by section if specified #} {% if section_filters %} {% set filtered_posts = [] %} {% for post in category_posts %} {% if post._section.name in section_filters %} {% do filtered_posts.append(post) %} {% endif %} {% endfor %} {% set category_posts = filtered_posts %} {% endif %} {% if category_posts %}

{{ category | title }}

{{ category_posts | length }} page{{ 's' if category_posts | length != 1 }}
{# Category stats #}
{% set total_words = category_posts | map(attribute='content') | join('') | wordcount %} {% set authors = category_posts | map(attribute='metadata.author') | select('defined') | unique | list %} 📖 {{ total_words | format_number }} words {% if authors %} ✍️ {{ authors | length }} author{{ 's' if authors | length != 1 }} {% endif %}
{# Recent posts in this category #} {% if show_recent > 0 %} {% set recent_posts = category_posts | sort_by('date', reverse=true) | limit(show_recent) %}

Recent:

    {% for post in recent_posts %}
  • {{ post.title }} {% if post.date %} {% endif %}
  • {% endfor %}
{% if category_posts | length > show_recent %} View all {{ category_posts | length }} → {% endif %}
{% endif %}
{% endif %} {% endfor %}
{# All Categories A-Z Index #}
{% endif %}
{{ letter }}
{% endif %} {% else %} {# Empty State #}

No categories found.

Add categories: ["tutorial", "guide"] to page frontmatter to create categories.

{% endif %} {% endblock %}