Template Integration =================== This guide covers how to properly integrate Martor in your Django templates, both for editing (forms) and displaying (rendered markdown) content. Template Tags and Filters -------------------------- Martor provides template tags and filters for rendering markdown content: .. code-block:: html {% load martortags %} Safe Markdown Filter ~~~~~~~~~~~~~~~~~~~~ The primary filter for rendering markdown as HTML: .. code-block:: html {% load martortags %} {{ post.content|safe_markdown }} {{ article.description|safe_markdown }} The ``safe_markdown`` filter: * Converts markdown to HTML * Applies security filtering (XSS protection) * Processes Martor extensions (emoji, mentions, etc.) * Returns safe HTML that can be displayed directly Template Structure for Forms ----------------------------- Basic Form Template ~~~~~~~~~~~~~~~~~~~ .. code-block:: html {% extends "base.html" %} {% load static %} {% block title %}Create Post{% endblock %} {% block css %} {% endblock %} {% block content %}

Create New Post

{% csrf_token %}
{{ form.title }} {% if form.title.errors %}
{% for error in form.title.errors %} {{ error }} {% endfor %}
{% endif %}
{{ form.content }} {% if form.content.help_text %}
{{ form.content.help_text }}
{% endif %} {% if form.content.errors %}
{% for error in form.content.errors %} {{ error }} {% endfor %}
{% endif %}
Cancel
{% endblock %} {% block js %} {% endblock %} Semantic UI Template ~~~~~~~~~~~~~~~~~~~~ .. code-block:: html {% extends "base.html" %} {% load static %} {% block css %} {% endblock %} {% block content %}

Create Post

{% csrf_token %}
{{ form.title }}
{{ form.content }}
{% endblock %} {% block js %} {% endblock %} Template Structure for Display ------------------------------ Basic Display Template ~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: html {% extends "base.html" %} {% load static %} {% load martortags %} {% block title %}{{ post.title }}{% endblock %} {% block css %} {% endblock %} {% block content %}

{{ post.title }}

Published on {{ post.created_at|date:"F d, Y" }} {% if post.author %}by {{ post.author.get_full_name|default:post.author.username }}{% endif %}

{{ post.content|safe_markdown }}
{% endblock %} {% block js %} {% endblock %} Advanced Display Templates ~~~~~~~~~~~~~~~~~~~~~~~~~~ Template with Table of Contents: .. code-block:: html {% extends "base.html" %} {% load static %} {% load martortags %} {% block css %} {{ block.super }} {% endblock %} {% block content %}

{{ post.title }}

{{ post.content|safe_markdown }}
Table of Contents
{% endblock %} {% block js %} {{ block.super }} {% endblock %} Multiple Content Display ~~~~~~~~~~~~~~~~~~~~~~~~~ For models with multiple markdown fields: .. code-block:: html {% extends "base.html" %} {% load static %} {% load martortags %} {% block content %}

{{ product.name }}

Description

{{ product.description|safe_markdown }}
{% if product.specifications %}

Technical Specifications

{{ product.specifications|safe_markdown }}
{% endif %} {% if product.usage_instructions %}

Usage Instructions

{{ product.usage_instructions|safe_markdown }}
{% endif %}
Product Info

Price: ${{ product.price }}
SKU: {{ product.sku }}

{% endblock %} AJAX Templates -------------- For dynamic content loading: .. code-block:: html Template Blocks and Includes ----------------------------- Reusable Template Blocks ~~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: html {% load static %} {% load static %} {% block css %} {% include "_martor_css.html" %} {% endblock %} {% block js %} {% include "_martor_js.html" %} {% endblock %} Form Field Include ~~~~~~~~~~~~~~~~~~ .. code-block:: html
{{ field }} {% if field.help_text %}
{{ field.help_text }}
{% endif %} {% if field.errors %}
{% for error in field.errors %} {{ error }} {% endfor %}
{% endif %}
{% with field=form.content %} {% include "_martor_field.html" %} {% endwith %} Template Performance -------------------- Optimization Techniques ~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: html {% extends "base.html" %} {% load static %} {% load martortags %} {% block css %} {% if form %} {% include "_martor_css.html" %} {% else %} {% endif %} {% endblock %} {% block js %} {% if form %} {% include "_martor_js.html" %} {% else %} {% endif %} {% endblock %} Caching Rendered Content ~~~~~~~~~~~~~~~~~~~~~~~~ .. code-block:: html {% load cache %} {% load martortags %} {% cache 3600 post_content post.id post.updated_at %}
{{ post.content|safe_markdown }}
{% endcache %} Best Practices -------------- 1. **Separate CSS/JS for editing vs. display**: .. code-block:: html {% if form %} {% include "_martor_editor_assets.html" %} {% endif %} {% if content %} {% include "_martor_display_assets.html" %} {% endif %} 2. **Use proper CSRF tokens**: .. code-block:: html
{% csrf_token %} {{ form.as_p }}
3. **Handle errors gracefully**: .. code-block:: html {% if form.errors %}
Please correct the errors below.
{% endif %} 4. **Provide loading states**: .. code-block:: html
Loading editor...
Next Steps ---------- * :doc:`../customization` - Advanced customization * :doc:`../examples/basic` - Complete template examples * :doc:`../themes` - Theming and styling * :doc:`../security` - Security considerations