Skip to main content

django-templatetag-handlebars allows you to embed Handlebars.js templates in your django templates.

Most of the template tag code comes from Miguel Araujo’s verbatim snippet.

INSTALL

pip install django-templatetag-handlebars

USAGE

  • Add templatetag_handlebars to your INSTALLED_APPS

  • Add the HTML header :

{% load templatetag_handlebars %}

<head>
{% handlebars_js %}
...
</head>
  • Call the template tag, and write your Handlebars template :

{% tplhandlebars "tpl-infos" %}
    {{total}} {% trans "result(s)." %}
    <p>{% trans "Min" %}: {{min}}</p>
    <p>{% trans "Max" %}: {{max}}</p>
{% endtplhandlebars %}
  • The following block with end-up in your page :

<script type="text/x-handlebars" id="tpl-infos">

    {{total}} result(s).
    <p>Min: {{min}}</p>
    <p>Max: {{max}}</p>
<script>
  • Render it, client-side, as usual using Handlebars.js API :

var properties = {
    total: 10,
    min: 4,
    max: 5
};

var template = Handlebars.compile($('#tpl-infos').html()),
    rendered = template(properties);
  • Your rendered string is ready, and waiting to be inserted in your DOM :)

10 result(s).
<p>Min: 4</p>
<p>Max: 5</p>

Advanced

A {% verbatim %} tag is available to escape a specific part. For example, you may want a subpart of your Handlebars template to be rendered by Django :

<script type="text/x-handlebars" id="tpl-django-form">

    <form>
        {% verbatim %}
            {{#if id}}<h1>{{first}} {{last}}</h1>{{/if}}

            {% trans "Your id is" %} {{ id }}
        {% endverbatim %}
        {{ yourform.as_p }}
    </form>
</script>

Using Ember.js

In settings.py ensure to set the following attribute to True. This is due to Ember.js expecting a slightly different script id declaration

USE_EMBER_STYLE_ATTRS = True

The script block will be rendered like `<script type="text/x-handlebars" data-template-name="%s">`.

AUTHORS

makinacom

Similar tools

LICENSE

  • Lesser GNU Public License

  • Handlebars.js is released under the MIT license - Copyright 2011 Yehuda Katz

CHANGELOG

1.3.1 (2016-01-04)

  • Add Python3 compatibility (thanks Tadas Dailyda)

1.3.0 (2015-05-26)

  • Add Django 1.8 compatibility (thanks @markvl)

1.2.0 (2012-05-30)

  • Add compatibility with Ember.js with a setting (default False). See README.rst.

1.1.0 (2012-03-26)

  • Remove dependency to template Egg loader using template tag

1.0.1 (2012-03-16)

  • Fix packaging

1.0.0 (2012-03-16)

  • Initial working version

Release files for django-templatetag-handlebars 1.3.1

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-templatetag-handlebars 1.3.1
File Size Uploaded
django-templatetag-handlebars-1.3.1.zip 28.3 kB Details

Release files / django-templatetag-handlebars-1.3.1.zip

Download URL django-templatetag-handlebars-1.3.1.zip
Size 28.3 kB
Tags Source
SHA-256 checksum
How to use checksums
7db0a35d541d11927cdd15dc8a2e7cebcd1421a4ca5cd948aa4fbe63d69936ff
BLAKE2b-256 checksum
How to use checksums
5565e1459f168ee58f7399b2b952728c766a5f49ea7d27a22346fa27d2f2db43
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.3.1 This release

1 release file

1.3.0

1 release file

1.2.0

1 release file

1.1.0

1 release file

1.0.1

1 release file

1.0

1 release file

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