Django Sniplates¶
Efficient template macros
Contents:
Overview¶
Have you ever found yourself repeating chunks of your templates, and wishing Django had a “macro” in templates?
Tried using {% include %} only to find it slow and clumsy?
Introducing Sniplates - the efficient way to provide your template authors with an arsenal of template macros.
Requirements¶
Requires Django 1.8 or newer, and is tested against Python 2.7, 3.3, 3.4, 3.5 and PyPy.
Quick-start¶
First, install sniplates:
pip install django-sniplates
And add it to INSTALLED_APPS
.
Next, write a template with your widgets. Let’s go with bootstrap tools, and call it widgets/bootstrap.html
{% block label %}<span class="label label-{{ label_type|default:'default' }}">{{ text }}</span>{% endblock %}
{% block alert %}<div class="alert alert-{{ alert_type|default:'success' }}" role="alert">{{ text }}</div>{% endblock %}
Now, in your main template, you need to load the sniplates library.
{% load sniplates %}
Then load your widget library, and give it an alias:
{% load_widgets bootstrap="widgets/bootstrap.html" %}
Now, when you need to add a label or alert in your page:
{% widget "bootstrap:label" text="Things go here" %}
{% widget "bootstrap:alert" text="It's alive" alert_type="info" %}
Indices and tables¶
Thanks¶
This project was originally inspired by a feature request by Stephan Sokolow.