Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/edoburu/django-capture-tag
A micro-library to capture output in Django templates
https://github.com/edoburu/django-capture-tag
django django-template-tag microlibrary
Last synced: about 21 hours ago
JSON representation
A micro-library to capture output in Django templates
- Host: GitHub
- URL: https://github.com/edoburu/django-capture-tag
- Owner: edoburu
- License: apache-2.0
- Created: 2016-04-20T12:19:46.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-07-11T08:22:28.000Z (over 1 year ago)
- Last Synced: 2024-10-30T15:24:16.567Z (16 days ago)
- Topics: django, django-template-tag, microlibrary
- Language: Python
- Homepage: http://django-capture-tag.readthedocs.org
- Size: 28.3 KB
- Stars: 20
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGES.rst
- License: LICENSE
- Authors: AUTHORS
Awesome Lists containing this project
README
django-capture-tag
==================.. image:: https://github.com/edoburu/django-capture-tag/actions/workflows/tests.yaml/badge.svg?branch=master
:target: https://github.com/edoburu/django-capture-tag/actions/workflows/tests.yaml
.. image:: https://img.shields.io/pypi/v/django-capture-tag.svg
:target: https://pypi.python.org/pypi/django-capture-tag/
.. image:: https://img.shields.io/pypi/l/django-capture-tag.svg
:target: https://pypi.python.org/pypi/django-capture-tag/
.. image:: https://img.shields.io/codecov/c/github/edoburu/django-capture-tag/master.svg
:target: https://codecov.io/github/edoburu/django-capture-tag?branch=masterA micro-library to capture output in Django templates.
This can be useful for example to:
* Repeat page titles in web pages, e.g. for the ```` tag and breadcrumb.
* Repeat contents for Social Media tags.
* Reusing thumbnail output in multiple places.
* Fetch configuration data from extended templates.Installation
------------Install the module from PyPI:
.. code-block:: bash
pip install django-capture-tag
Add the package to ``INSTALLED_APPS``:
.. code-block:: python
INSTALLED_APPS += (
'capture_tag',
)Load the tag in your template:
.. code-block:: html+django
{% load capture_tags %}
Syntax
------The following options are available:
.. code-block:: html+django
{% capture %}...{% endcapture %} # output in {{ capture }}
{% capture silent %}...{% endcapture %} # output in {{ capture }} only
{% capture as varname %}...{% endcapture %} # output in {{ varname }}
{% capture as varname silent %}...{% endcapture %} # output in {{ varname }} onlyExample usage
-------------To capture Social Media tags:
.. code-block:: html+django
{% load capture_tags %}
...{# Allow templates to override the page title/description #}
{% capture as meta_title %}{% block meta-title %}Untitled{% endblock %}{% endcapture %}{# display the same value as default, but allow templates to override it. #}
Take configuration from extended templates:
.. code-block:: html+django
# base.html
{% load capture_tags %}
# read once
{% capture as home_url silent %}{% block home_url %}{% url 'app:index' %}{% endblock %}{% endcapture %}# reuse twice.
Back to home
Back to home# child.html
{% extends "base.html" %}{% block home_url %}{% url 'user:profile' %}{% endblock %}
Notice
~~~~~~When a value is used only once, this package is not needed.
In such case, simply place the ``{% block .. %}`` at the proper location where contents is replaced.
All common Django template tags support the ``as variable`` syntax,
such as ``{% url 'app:index' as home_url %}`` or ``{% trans "Foo" as foo_label %}``.