Git Product home page Git Product logo

djangobars's Introduction

djangobars

An extension to allow Django to use Handlebars templates through the pybars port of Handlebars.js

Because don't we all want to use the same templates on the client that we do on the server?

NOTE: This project is very early-stage. Try it out, leave feedback and wishes in the issues. And pull-requests welcome!

Installation

  1. pip install djangobars
  2. Add 'djangobars' to your installed applications.
  3. Add a HANDLEBARS_LOADERS value to your settings module. You will probably want:

    HANDLEBARS_LOADERS = (
        'djangobars.template.loaders.filesystem.Loader',
        'djangobars.template.loaders.app_directories.Loader',
    )
  4. (optional) Add a HANDLEBARS_DIRS value to your setting module. By default, djangobars will search in your TEMPLATE_DIRS folder, but you can use the HANDLEBARS_DIRS value to override this behavior. For example, if you want to use both Django templates and Handlebars templates, you may want to keep the two in separate directories.

Usage

Use pretty much just as you would Django's own built-in templates. Instead of:

from django.shortcuts import render

def my_view(request):
    # View code here...
    return render(request, 'myapp/index.html', {"foo": "bar"},
        content_type="application/xhtml+xml")

do this:

from djangobars.shortcuts import render

def my_view(request):
    # View code here...
    return render(request, 'myapp/handlebar_index.html', {"foo": "bar"},
        content_type="application/xhtml+xml")

And instead of:

from django.views.generic import TemplateView

class MyView (TemplateView):
    template_name = 'myapp/index.html'

do this:

from django.views.generic import TemplateView
from djangobars.response import HandlebarsResponse

class MyView (TemplateView):
    template_name = 'myapp/handlebar_index.html'
    response_class = HandlebarsResponse

Template Tags

You can also include Handlebars templates with a Django template tag:

{% load djangobars %}

{% include_handlebars "handlebars_template_name.html" %}

The current template context will be carried over into the Handlebars template.

djangobars's People

Contributors

mjumbewu avatar

Watchers

 avatar  avatar

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.