Skip to main content
https://img.shields.io/travis/edoburu/django-capture-tag/master.svg?branch=master https://img.shields.io/pypi/v/django-capture-tag.svg https://img.shields.io/pypi/dm/django-capture-tag.svg https://img.shields.io/badge/wheel-yes-green.svg https://img.shields.io/pypi/l/django-capture-tag.svg https://img.shields.io/codecov/c/github/edoburu/django-capture-tag/master.svg

A 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 <title> 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:

pip install django-capture-tag

Add the package to INSTALLED_APPS:

INSTALLED_APPS += (
    'capture_tag',
)

Load the tag in your template:

{% load capture_tags %}

Syntax

The following options are available:

{% 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 }} only

Example usage

To capture Social Media tags:

{% load capture_tags %}

<head>
    ...

    {# Allow templates to override the page title/description #}
    <meta name="description" content="{% capture as meta_description %}{% block meta-description %}{% endblock %}{% endcapture %}" />
    <title>{% capture as meta_title %}{% block meta-title %}Untitled{% endblock %}{% endcapture %}</title>

    {# display the same value as default, but allow templates to override it. #}
    <meta property="og:description" content="{% block og-description %}{{ meta_description }}{% endblock %}" />
    <meta name="twitter:title" content="{% block twitter-title %}{{ meta_title }}{% endblock %}" />
</head>

Take configuration from extended templates:

# base.html

{% load capture_tags %}

# read once
{% capture as home_url silent %}{% block home_url %}{% url 'app:index' %}{% endblock %}{% endcapture %}

# reuse twice.
<a href="{{ home_url }}" class="btn page-top">Back to home</a>
<a href="{{ home_url }}" class="btn page-bottom">Back to home</a>

# 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 %}.

Release files for django-capture-tag 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-capture-tag 1.0
File Size Uploaded
django-capture-tag-1.0.tar.gz 8.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-capture-tag 1.0
File Interpreter ABI Platform
django_capture_tag-1.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 15.1 kB

Release files / django-capture-tag-1.0.tar.gz

Download URL django-capture-tag-1.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9c8a531687aac2a705a16a96c33930fb8193c17b641b7c24cd97ff180053d539
BLAKE2b-256 checksum
How to use checksums
ef880f8125b5d48c498b2009e472842628a458648b167ba1bdbdf09eb1d5937c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_capture_tag-1.0-py2.py3-none-any.whl

Download URL django_capture_tag-1.0-py2.py3-none-any.whl
Size 6.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a996f64996830c00641b5b41503c62616f9613a478c84a303fc414e96bfb4891
BLAKE2b-256 checksum
How to use checksums
626fc00e3e52de91debfb09c1d197da0d229143047d4f2d81f9f1a6272d2b8f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page