Skip to main content

A Django Template Tag that stashes content for dynamic reuse.

Project description

Django Stash Tag

A Django template tag that allows for markup to be stashed and dynamically applied without needing to create a separate partial or template tag file.

Installation

Install Django Stash Tag:

pip install django-stash-tag

After you've installed the tag, add django_stash_tag to INSTALLED_APPS in your settings.py file:

# settings.py

INSTALLED_APPS = (
    # …
    'django_stash_tag',
)

Static Usage

The stash tag can be used to store static markup for reuse further down the template. Using stash_apply you can render the stashed markup.

First we load the tag and stash the markup with a name to stash it under:

{% load stash %}

{% stash 'static_heading' %}
  <h1>User: {{user.name}}</h1>
{% endstash %}

We then apply the stash with stash_apply and provide the stash name static_heading as a tag argument:

{% stash_apply 'static_heading' %}

This would result in rendering (context.user is {'name': 'Ivan'}):

<h1>User: Ivan</h1>

Dynamic Usage

In most cases you probably need more dynamic control over the paramaterization of the stashed markup. You can accomplish such behavior through the use of template tag kwargs.

Again we load the tag and stash the markup. This time the context variables that are referenced within the stashed content are not defined in the template's context:

{% load stash %}

{% stash 'section_header' %}
  <header>
    <h3>{{title}} {{company}}</h2>
    <small>{{subtext}} {{company}}.</small>
  </header>
{% endstash %}

We then apply the stash with additional template kwargs pertaining to the context variables referenced in the stashed content (context.company set to {'name': 'GitHub'}):

{% stash_apply 'section_header'
    title='About'
    subtext='We will take you back to when it all began at'
%}

{# further down in the template #}

{% stash_apply 'section_header'
    title='Apply for a Position at'
    subtext='Make software development a better experience at'
%}

Which would then output:

<header>
  <h3>About GitHub</h2>
  <small>We will take you back to when it all began at GitHub.</small>
</header>

<!-- further down in template --> 

<header>
  <h3>Apply for a Position at GitHub</h2>
  <small>Make software development a better experience at GitHub.</small>
</header>

As demonstrted above you can use stash_apply as much as you want once you've stashed the content in your template. You can use both context variables already set on the template or dynamic variables set though kwargs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-stash-tag-19.3.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

django_stash_tag-19.3.0-py2-none-any.whl (3.6 kB view details)

Uploaded Python 2

File details

Details for the file django-stash-tag-19.3.0.tar.gz.

File metadata

  • Download URL: django-stash-tag-19.3.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

File hashes

Hashes for django-stash-tag-19.3.0.tar.gz
Algorithm Hash digest
SHA256 37d52d133c27995d65028f77859cf2f98c57a65023d5ab9bb5639abf85360bf8
MD5 4ec07d17eefb590845c0040c566ea4f5
BLAKE2b-256 23cc048e28302b6f7faf80dd53363f110101143cbf184df626f774524d5b18d3

See more details on using hashes here.

File details

Details for the file django_stash_tag-19.3.0-py2-none-any.whl.

File metadata

  • Download URL: django_stash_tag-19.3.0-py2-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

File hashes

Hashes for django_stash_tag-19.3.0-py2-none-any.whl
Algorithm Hash digest
SHA256 e280246735c314010bbc479f216403946f07cae652a48c969c9269d6db817014
MD5 40a89ef2544f015526e322af5e6cc65c
BLAKE2b-256 f79aca52d9542b3d5e284cf8dd0e59deb35fd4b019e54da2b64b214bd5d231a4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page