Handlebars.js in Django templates

前端 未结 8 1565

I need a javascript templating system and i think handlebars.js does an excellent job in this case. I\'m having syntax conflicts with handlebars templates inside a django te

相关标签:
8条回答
  • 2020-12-24 06:36

    Compile your handlebars first!

    From handlebars precompiling documentation:

    In addition to reducing the download size, eliminating client-side compilation will significantly speed up boot time, as compilation is the most expensive part of Handlebars.

    You can compile templates in your build environment using handlebars npm module, or integrate it with a build tool like gulp with gulp-handlebars.

    After compiling, your handlebars templates can be served as static resources and bypass server side rendering altogether. Makes it easier on caching too :)

    Typical usage would look like this:

    <div id="restaurants-tpl">
        Waiting for content...
    </div>
    
    <script src="{% static 'js/handlebars.runtime.js' %}"></script>
    <script src="{% static 'js/templates.js' %}"></script>
    <script>
        // Let django render this as a json string
        properties = {{ properties }};
    
        // Use Handlebars compiled template imported above
        rendered_html = Handlebars.templates["restaurants-tpl"](properties);
    
        // Set element content 
        document.getElementById("restaurants-tpl").innerHTLM = rendered_html;
    </script>

    0 讨论(0)
  • 2020-12-24 06:42

    I use a custom template tag for another js templating system, here: https://gist.github.com/629508

    Use in template:

    {% load mytags %}
    {% verbatim %}
      {{ This won't be touched by {% django's %} template system }}
    {% endverbatim %}
    

    Edit: This custom template tag is no longer necessary, as Django's template language now supports the {% verbatim %} template tag.

    0 讨论(0)
  • 2020-12-24 06:44

    Is there a tag in django templates to stop rendering a block with curly braces?

    OLD Answer for Django 1.0-1.4: No, though you could though you could put the block in a separate file and include it without rendering or use a different templating engine.

    New Answer: The answer above was correct in August 2011 when the question was asked and answered. Starting in Django 1.5 (released Feb 2013, though alpha/beta versions in late 2012), they introduced the {% verbatim %} and {% endverbatim %} which will prevent the django template engine from processing the content in the block.

    So for the question asked the following will work in django 1.5+ out of the box:

    {{ django_context_varable }} #works
    {% verbatim %}
    <script id="restaurants-tpl" type="text/x-handlebars-template">
        <ul>
        {{#restaurants}} #not rendered by django, plain text
        <li>{{name}}</li>
        {{/restaurants}}
        </ul>
    </script>
    {% endverbatim %}
    

    The documentation on verbatim is here. Yes, this was noted by others earlier, but as this is the accepted answer I should list the easiest solution.

    0 讨论(0)
  • 2020-12-24 06:46

    Why not use jinja2 instead? IMO, they're both elegant to use. Here's an excellent article about it: Using Jinja2 with Django

    0 讨论(0)
  • 2020-12-24 06:53

    I wrote a very small django application : django-templatetag-handlebars exactly for that purpose.

    {% load templatetag_handlebars %}
    
    {% tplhandlebars "tpl-infos" %}
        {{total}} {% trans "result(s)." %}
        <p>{% trans "Min" %}: {{min}}</p>
        <p>{% trans "Max" %}: {{max}}</p>
    {% endtplhandlebars %}
    

    Render your block as usual using Handlebars.js API :

    var properties = {
        total: 10,
        min: 5,
        max: 4
    };
    
    var template = Handlebars.compile($('#tpl-infos').html()),
        rendered = template(properties);
    

    I wrote it the day @chrisv published its package, with a KISS approach in mind. It is mainly based on Miguel Araujo's gist : https://gist.github.com/893408.

    0 讨论(0)
  • 2020-12-24 06:56

    for a deeper integration between handlebars and Django (including optional on-the-fly precompilation) check out my project at https://bitbucket.org/chrisv/django-handlebars/

    It basically works like this:

    1. create HB template under

      appdirectory/hbtemplates/myapp/template.html
      

      (just like Django template)

    2. in your app, use

      {% handlebars myapp %} 
      

      template tag and render template like so:

      Handlebars.templates["myapp.template.html"]({context:"value"});
      
    0 讨论(0)
提交回复
热议问题