Giter Site home page Giter Site logo

mjumbewu / django-mustachejs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from carljm/django-icanhaz

17.0 3.0 6.0 255 KB

Embed mustache-like Javascript templates into Django templates with minimal fuss.

License: Other

JavaScript 30.83% Python 69.17%

django-mustachejs's Introduction

NOTE: django-mustachejs is now named "django-jstemplate"!

The source is available at http://github.com/mjumbewu/django-jstemplate, and the package is at http://pypi.python.org/pypi/django-jstemplate/. Please update your references.

Migration is easy:

  • In your settings' INSTALLED_APPS, mustachejs becomes jstemplate
  • MUSTACHEJS_... settings become JSTEMPLATE_...
  • In your Django templates, {% load mustachejs %} becomes {% load jstemplate %}

That's it. If you have any issues, get in touch with me on GitHub or on Twitter @mjumbewu. Thanks for using the project!

django-mustachejs

build status

A templatetag framework for easier integration of mustache.js JavaScript templates with Django templates. Inspired by ICanHaz.js, django-icanhaz, and jquery.mustache.

NOTE: Though django-mustachejs was originally developed for mustache-style templates, it can actually be used to embed any type of client-side templates. It might be better named django-jstemplates or something like that.

Quick Usage

(Read the full docs on Read the Docs)

Add "mustachejs" to your INSTALLED_APPS setting.

app/jstemplates/main.mustache:

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

app/templates/main.html:

{% load mustachejs %}

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.js"></script>
  <script src="{{ STATIC_URL }}mustache/js/mustache-0.3.0.js"></script>
  <script src="{{ STATIC_URL }}mustache/js/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 (from django-icanhaz)

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 my contribution to the array of solutions is essentially just an "include" tag that avoids parsing the included file as a Django template (and for convenience, automatically wraps it in the script tag that ICanHaz.js expects to find it in).

Enjoy!

django-mustachejs's People

Contributors

achur avatar carljm avatar gehan avatar ghinch avatar mjumbewu avatar slinkp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-mustachejs's Issues

Merge in the ability to use regexes to match templates

Merge in the regex awesomeness from achur@0df257f

Then, in your template, instead of saying:

{% mustachejs "thispage/file1" %}
{% mustachejs "thispage/file2" %}
{% mustachejs "thispage/file3" %}
{% mustachejs "thispage/file4" %}

you could just say:

{% mustachejs "thispage/" ".*" %}

No more remembering to add or remove each template from a long list of templates!

Allow comments for translators

I was in a messages file today and I saw this:

msgid "{{ prompt }}{{# optional }} (optional){{/ optional }}"
msgstr ""

I really wished I had some context. We should allow developers to give translator hints (like this). I imagine, in a template, it would look something like:

{{! Translators: This is the label for a survey form field. }}
{{#_}}{{ prompt }}{{# optional }} (optional){{/ optional }}{{/_}}

And in the .po file, it would show up like:

# Translators: This is the label for a survey form field.
# my/file.html:24
msgid "{{ prompt }}{{# optional }} (optional){{/ optional }}"
msgstr ""

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.