Skip to main content

A Django template tag for embedding Mustache.js -- or other JavaScript templates -- templates safely.

Project description

=================
django-jstemplate
=================

|build status|_

.. |build status| image:: https://secure.travis-ci.org/mjumbewu/django-jstemplate.png
.. _build status: https://secure.travis-ci.org/mjumbewu/django-jstemplate

A templatetag framework for easier integration of `mustache.js`_, `dust.js`_,
`handlebars.js`_, or other JavaScript templates with Django templates. Also will
wrap your templates in elements expected for libraries such as `ICanHaz.js`_.
Django-jstemplates is extensible, so if your favorite template library is not
included, it's easy to add. Inspired by `django-icanhaz`_.

.. _mustache.js: http://mustache.github.com/
.. _dust.js: http://akdubya.github.com/dustjs/
.. _handlebars.js: http://handlebarsjs.com/
.. _ICahHaz.js: http://icanhazjs.com/
.. _django-icanhaz: http://github.com/carljm/django-icanhaz

Quick Usage
-----------

(Read the full docs on `Read the Docs`_)

.. _Read the Docs: http://django-mustachejs.readthedocs.org/en/latest/

Add ``"jstemplate"`` to your ``INSTALLED_APPS`` setting.

``app/jstemplates/main.mustache``::

<div>
<p>This is {{ name }}'s template</p>
</div>

``app/templates/main.html``::

{% load jstemplate %}

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
<script src="{{ STATIC_URL }}libs/mustache-0.3.0.js"></script>
<script src="{{ STATIC_URL }}libs/django.mustache.js"></script>
</head>

<body>
<div id="dynamic-area"></div>

{% mustachejs "main" %}

<script>
$(document).ready(function() {

var $area = $('#dynamic-area')
, template;

template = Mustache.template('main');
$area.html(template.render());

});
</script>
</body>
</html>


Rationale
---------

The collision between Django templates' use of ``{{`` and ``}}`` as template
variable markers and `mustache.js`_' use of same has spawned a variety of
solutions. `One solution`_ simply replaces ``[[`` and ``]]`` with ``{{`` and
``}}`` inside an ``mustachejs`` template tag; `another`_ makes a valiant attempt
to reconstruct verbatim text within a chunk of a Django template after it has
already been mangled by the Django template tokenizer.

I prefer to keep my JavaScript templates in separate files in a dedicated
directory anyway, to avoid confusion between server-side and client-side
templating. So this solution is essentially just an "include" tag that avoids
parsing the included file as a Django template.

Enjoy!

.. _one solution: https://gist.github.com/975505
.. _another: https://gist.github.com/629508


CHANGES
=======

1.0.0
------------------

* Rename project to "django-jstemplate" (from "django-mustachejs")
* Rename the "mustacheraw" template tag to "rawjstemplate"
* Rename the "mustacheich" template tag to "icanhazjs"

0.8.3
------------------

* Fix incorrect identifier name in makemessages command

0.8.2
------------------

* Fix the FilesystemRegexFinder to match exactly the extensions specified in
the configuration, and no more

0.8.1
------------------

* Fix makemessages to work with recent code updates

0.8.0
------------------

* Have the regex finder respect the MUSTACHEJS_EXTS setting
* Update normal file finders to use glob patterns

0.7.4
------------------

* Fix i18n preprocessor for python 2.6 compatibility

0.7.3
------------------

* Correctly escaped translated strings.

0.7.2
------------------

* Changed search pattern to be non-greedy.

0.7.1
------------------

* Handle translation strings with new lines.

0.7.0
------------------

* Pulled in commit from django-icanhaz to load templates using regular
expressions.

* Added preprocessing framework, and a preprocessor for i18n.

* Hijack the makemessages command to find js template messages as well.

0.6.0
------------------

* Add ``dustjs`` tag to insert a script block to create a compiled dustjs
template. Thanks to `Gehan Gonsalkorale <https://github.com/gehan>`_.

0.5.0
------------------

* Add ``mustacheraw`` tag to insert just the raw text of a mustacehe template.
Thanks to Greg Hinch.

* Add ``mustacheich`` tag to insert a mustache script block as icanhaz expects.

0.4.1 (2012.01.09)
------------------

* Fixed template reading to explicitly decode template file contents using
Django's ``FILE_CHARSET`` setting. Thanks Eduard Iskandarov.

* Fixed template-finding failure with non-normalized directories in
``MUSTACHEJS_DIRS``. Thanks Eduard Iskandarov for report and patch.


0.4.0
------------------

* Add the MUSTACHEJS_EXTS configuration variable for specifying the extensions
allowed for template files located by the FilesystemFinder (and, by extension,
the AppFinder).


0.3.3
------------------

* Add a package_data value to the setup call


0.3.2
------------------

* Add the MANIFEST.in file itself as an entry in MANIFEST.in.


0.3.0
------------------

* Change the name from django-icanhaz to django-mustachejs.
* Remove dependency on ICanHaz.js. I like the library, but the maintainers
were not responsive enough for now. Use Mustache.js straight, with a little
bit of minimal sugar. Templates are rendered to straight Javascript.


0.2.0 (2011.06.26)
------------------

* Made template-finding more flexible: ``ICANHAZ_DIR`` is now ``ICANHAZ_DIRS``
(a list); added ``ICANHAZ_FINDERS``, ``ICANHAZ_APP_DIRNAMES``, and finding of
templates in installed apps.


0.1.0 (2011.06.22)
------------------

* Initial release.


TODO
====

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-jstemplate-1.0.0.tar.gz (14.1 kB view hashes)

Uploaded Source

Supported by

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