GithubHelp home page GithubHelp logo

rmoorman / jingo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jbalogh/jingo

0.0 3.0 0.0 113 KB

An adapter for using Jinja2 templates with Django.

Home Page: http://readthedocs.org/docs/jingo/en/latest/

License: BSD 3-Clause "New" or "Revised" License

jingo's Introduction

Jingo

Jingo is an adapter for using Jinja2 templates within Django.

Note

Coffin or Jingo?

Jingo differs from Coffin in two major ways:

  • Jingo serves purely as a minimalistic bridge between Django and Jinja2. Coffin attempts to reduce the differences between Jinja2 templates and Django's native templates.
  • Jingo has a far supperior name, as it is a portmanteau of 'Jinja' and Django.

Usage

When configured properly (see Settings below) you can render Jinja2 templates in your view the same way you'd render Django templates:

from django.shortcuts import render


def MyView(request):
    # TODO: Do something.
    context = dict(user_ids=[1, 2, 3, 4])
    render('users/search.html', context)

..note:

Not only does ``django.shorcuts.render`` work, but so does any method that
Django provides to render templates.

Settings

You'll want to use Django to use jingo's template loader. In settings.py:

TEMPLATE_LOADERS = (
    'jingo.Loader',
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)

This will let you use django.shortcuts.render or django.shortcuts.render_to_response.

And finally you may have apps that do not use Jinja2, these must be excluded from the loader:

JINGO_EXCLUDE_APPS = ('debug_toolbar',)

If a template path begins with debug_toolbar, the Jinja loader will raise a TemplateDoesNotExist exception. This causes Django to move onto the next loader in TEMPLATE_LOADERS to find a template - in this case, django.template.loaders.filesystem.Loader.

Note

Technically, we're looking at the template path, not the app. Often these are the same, but in some cases, like 'registration' in the default setting--which is an admin template--they are not.

The default is in jingo.EXCLUDE_APPS:

EXCLUDE_APPS = (
    'admin',
    'admindocs',
    'registration'
)

If you want to configure the Jinja environment, use JINJA_CONFIG in settings.py. It can be a dict or a function that returns a dict. :

JINJA_CONFIG = {'autoescape': False}

or :

def JINJA_CONFIG():
    return {'the_answer': 41 + 1}

Template Helpers

Instead of template tags, Jinja encourages you to add functions and filters to the templating environment. In jingo, we call these helpers. When the Jinja environment is initialized, jingo will try to open a helpers.py file from every app in INSTALLED_APPS. Two decorators are provided to ease the environment extension:

jingo.register.filter

Adds the decorated function to Jinja's filter library.

jingo.register.function

Adds the decorated function to Jinja's global namespace.

jinja

Default Helpers

Helpers are available in all templates automatically, without any extra loading.

jingo.helpers

Template Environment

A single Jinja Environment is created for use in all templates. This is available as jingo.env if you need to work with the Environment.

Localization

Since we all love L10n, let's see what it looks like in Jinja templates:

<h2>{{ _('Reviews for {0}')|f(addon.name) }}</h2>

The simple way is to use the familiar underscore and string within a {{ }} moustache block. f is an interpolation filter documented below. Sphinx could create a link if I knew how to do that.

The other method uses Jinja's trans tag:

{% trans user=review.user|user_link, date=review.created|datetime %}
  by {{ user }} on {{ date }}
{% endtrans %}

trans is nice when you have a lot of text or want to inject some variables directly. Both methods are useful, pick the one that makes you happy.

Forms

Django marks its form HTML "safe" according to its own rules, which Jinja2 does not recognize.

jingo.monkey

Testing

Testing is handle via fabric:

fab test

jingo's People

Contributors

davedash avatar jbalogh avatar jsocol avatar clouserw avatar jlongster avatar lmorchard avatar mccammos avatar

Watchers

Rico Moorman avatar James Cloos 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.