Coder Social home page Coder Social logo

django-templatetag-handlebars's Introduction

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

django-templatetag-handlebars's People

Contributors

leplatrem avatar skirsdeda avatar stard0g avatar

Stargazers

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

Watchers

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

django-templatetag-handlebars's Issues

about django include tag

i want to include my jstemplate file like tpl_a.html which was wroten some html and used tplhandlebar tag, when i include this tpl file into another tpl, it can't work correctly, after rending found nothing.
sample code:
tpl_a.html
{% tplhandlebars "product form" %}

...........{{product}}............
{% endtplhandlebars %}
some_form.html

hello
{% include "tpl_a.html" %}

after rendering i found nothing about tpl_a.html in my page some_form.html, just a word hello

Django 1.11 and newer requires mark_safe() on template tag results

I have seen the problem in templatetag_handlebars.js:handlebars_js(). Without mark_safe() it's output is quoted and appears as text in the pages using the tag. Corrected version:

@register.simple_tag
def handlebars_js():
return mark_safe("""<script src="%shandlebars.js"></script>""" % settings.STATIC_URL)

Django >=2.1 compatibility

templatetag_handlebars/templatetags/templatetag_handlebars.py", line 37, in verbatim_tags if token.token_type == template.base.TOKEN_VAR: AttributeError: module 'django.template.base' has no attribute 'TOKEN_VAR'

in Django 2.1 django.template.base has been changed

types of interest:

  • in Django 2.0: plain text (TOKEN_TEXT), variables (TOKEN_VAR) or block statements
    (TOKEN_BLOCK)

  • in Django 2.1: plain text (TokenType.TEXT), variables (TokenType.VAR), or block statements (TokenType.BLOCK)

please apply changes to get Django 2.2 compatibility

Quotes in the tplhandlebars tag

When using single quotes instead of double for specifying the id of template ({% tplhandlebars 'some-id' %}), the generated code has something like: id="'some-id'", while it works perfectly fine with double quotes -- it doesn't put additional quotes.

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.