.. _custom_templates: ########################### How to create custom templates ########################### This guide shows how to create and customize templates for your stories, including complete template sets and plugin templates. Base Template Structure ======================= djangocms-stories uses a base template system for easy customization. All story templates extend a ``djangocms_stories/base.html`` template, which in turn extends your site's ``base.html`` template. **Template hierarchy:** :: your_site/base.html └── djangocms_stories/base.html ├── djangocms_stories/post_list.html ├── djangocms_stories/post_detail.html ├── djangocms_stories/category_list.html └── other story templates... Creating a Custom Base Template ================================ If your site's ``base.html`` doesn't have a ``content`` block, or you need different structure, create your own base template: **templates/djangocms_stories/base.html:** :: {% extends "base.html" %} {% load cms_tags %} {% block title %}{{ block.super }}{% endblock %} {% block content %}
{% block stories_content %}{% endblock %}
{% endblock %} {% block extra_css %} {{ block.super }} {% endblock %} Complete Template Sets ====================== You can create a complete custom template set for different looks or configurations. Setting Up Template Sets ------------------------- 1. **Create template directory structure:** :: templates/ my_stories/ # Custom template set base.html post_list.html post_detail.html category_list.html tag_list.html plugins/ latest_entries.html categories.html tags.html archive.html 2. **Configure in Stories Config:** In Django admin, go to **Stories → Configurations** and set: - **Template prefix:** ``my_stories`` 3. **Copy and customize templates:** Start by copying the default templates: :: cp -a djangocms_stories/templates/djangocms_stories/* /path/to/your/project/templates/my_stories/ Custom Story List Template =========================== Create a customized story list view: **templates/my_stories/post_list.html:** :: {% extends "my_stories/base.html" %} {% load i18n cms_tags stories_tags thumbnail %} {% block title %}{{ view.get_title }} - {{ block.super }}{% endblock %} {% block stories_content %}

{{ view.get_title }}

{% if category %}

{{ category.description }}

{% endif %}
{% get_categories as all_categories %}
{% trans "Categories:" %} {% trans "All" %} {% for cat in all_categories %} {{ cat.name }} ({{ cat.post_count }}) {% endfor %}
{% for post in post_list %}
{% if post.main_image %}
{% thumbnail post.main_image 400x250 crop quality=95 as thumb %} {{ post.title }}
{% if post.categories.exists %} {% for cat in post.categories.all %} {{ cat.name }} {% endfor %} {% endif %}
{% endif %}

{{ post.title }}

{% if post.abstract %}

{{ post.abstract|truncatewords:25 }}

{% endif %} {% if post.tags.exists %} {% endif %}
{% endfor %}
{% if is_paginated %} {% endif %}
{% endblock %} Custom Story Detail Template ============================= Create an enhanced story detail view: **templates/my_stories/post_detail.html:** :: {% extends "my_stories/base.html" %} {% load i18n cms_tags stories_tags meta_tags thumbnail %} {% block title %}{{ post.title }} - {{ block.super }}{% endblock %} {% block meta %}{{ post.as_meta }}{% endblock %} {% block extra_head %} {% endblock %} {% block stories_content %}
{% if post.categories.exists %}
{% for category in post.categories.all %} {{ category.name }} {% endfor %}
{% endif %}

{{ post.title }}

{% if post.abstract %}

{{ post.abstract }}

{% endif %} {% if post.main_image %}
{% thumbnail post.main_image 1200x600 crop quality=95 as thumb %} {{ post.title }}
{% endif %}
{% render_placeholder post.content %}
{% get_related_posts post as related_posts %} {% if related_posts %} {% endif %} {% endblock %} Plugin Templates ================ Create custom templates for story plugins to match your design. Custom Latest Entries Plugin ----------------------------- **templates/my_stories/plugins/latest_entries.html:** :: {% load i18n cms_tags stories_tags thumbnail %}
{% if instance.template_folder_name %}

{{ instance.title|default:_("Latest Stories") }}

{% endif %}
{% for post in posts_list %}
{% if post.main_image and instance.image %}
{% thumbnail post.main_image 150x100 crop as thumb %} {{ post.title }}
{% endif %}
{% endfor %}
{% if instance.more %} {% endif %}
Template Sets with STORIES_PLUGIN_TEMPLATE_FOLDERS =================================================== Define multiple template sets for different plugin layouts: **settings.py:** :: STORIES_PLUGIN_TEMPLATE_FOLDERS = ( ('plugins', _('Default template')), # templates/my_stories/plugins/ ('timeline', _('Timeline layout')), # templates/my_stories/timeline/ ('cards', _('Card layout')), # templates/my_stories/cards/ ('minimal', _('Minimal layout')), # templates/my_stories/minimal/ ) **Timeline Layout Example:** **templates/my_stories/timeline/latest_entries.html:** :: {% load i18n cms_tags stories_tags %}
{% for post in posts_list %}

{{ post.title }}

{% if instance.abstract %}

{{ post.abstract|truncatewords:20 }}

{% endif %}
{% endfor %}
CSS for Custom Templates ========================= Add styles to support your custom templates: **static/css/stories-custom.css:** :: /* Stories Container */ .stories-container { max-width: 1200px; margin: 0 auto; padding: 2rem 1rem; } /* Stories Grid */ .stories-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(350px, 1fr)); gap: 2rem; margin: 2rem 0; } /* Story Cards */ .story-card { background: #fff; border-radius: 12px; overflow: hidden; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); transition: transform 0.3s ease, box-shadow 0.3s ease; } .story-card:hover { transform: translateY(-4px); box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15); } /* Story Images */ .story-image { position: relative; height: 250px; overflow: hidden; } .story-image img { width: 100%; height: 100%; object-fit: cover; } .image-overlay { position: absolute; top: 1rem; left: 1rem; } .category-badge { background: rgba(0, 0, 0, 0.8); color: white; padding: 0.25rem 0.75rem; border-radius: 20px; font-size: 0.875rem; margin-right: 0.5rem; } /* Story Content */ .story-content { padding: 1.5rem; } .story-title a { color: #2c3e50; text-decoration: none; font-weight: 600; line-height: 1.3; } .story-title a:hover { color: #3498db; } .story-abstract { color: #7f8c8d; margin: 1rem 0; line-height: 1.6; } /* Story Meta */ .story-meta { display: flex; align-items: center; gap: 1rem; font-size: 0.875rem; color: #95a5a6; margin: 1rem 0; } .story-meta time { font-weight: 500; } /* Tags */ .story-tags, .tag-list { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 1rem; } .tag-link { background: #ecf0f1; color: #2c3e50; padding: 0.25rem 0.75rem; border-radius: 15px; text-decoration: none; font-size: 0.875rem; transition: background-color 0.2s; } .tag-link:hover { background: #3498db; color: white; } /* Pagination */ .pagination { display: flex; justify-content: space-between; align-items: center; margin: 3rem 0; padding: 2rem 0; border-top: 1px solid #ecf0f1; } .pagination-links { display: flex; gap: 0.5rem; } .pagination-link { padding: 0.75rem 1rem; background: #ecf0f1; color: #2c3e50; text-decoration: none; border-radius: 6px; transition: all 0.2s; } .pagination-link:hover, .pagination-link.current { background: #3498db; color: white; } /* Responsive Design */ @media (max-width: 768px) { .stories-grid { grid-template-columns: 1fr; gap: 1rem; } .story-meta { flex-direction: column; align-items: flex-start; gap: 0.5rem; } .pagination { flex-direction: column; gap: 1rem; } } Template Override Priority ========================== Template resolution follows this order: 1. **Project templates** (highest priority) ``your_project/templates/my_stories/post_list.html`` 2. **App templates with prefix** ``djangocms_stories/templates/my_stories/post_list.html`` 3. **Default app templates** (lowest priority) ``djangocms_stories/templates/djangocms_stories/post_list.html`` This allows you to override only specific templates while using defaults for others. Best Practices ============== 1. **Start with copies** - Copy default templates and modify incrementally 2. **Use semantic CSS classes** - Make your templates maintainable 3. **Test responsive design** - Ensure templates work on all devices 4. **Optimize images** - Use appropriate thumbnail sizes 5. **Consider accessibility** - Add proper ARIA labels and semantic HTML