Giter VIP home page Giter VIP logo

django-icanhaz's Introduction

django-icanhaz

A templatetag for easier integration of ICanHaz.js JavaScript templates with Django templates.

Quickstart

Dependencies

Tested with Django 1.3 through trunk, and Python 2.6 and 2.7. Almost certainly works with older versions of both.

Installation

Install from PyPI with pip:

pip install django-icanhaz

or get the in-development version:

pip install django-icanhaz==dev

Usage

  • Add "icanhaz" to your INSTALLED_APPS setting.
  • Set the ICANHAZ_DIRS setting to a list of full (absolute) path to directories where you will store your ICanHaz templates.
  • {% load icanhaz %} and use {% icanhaz "templatename" %} in your Django templates to safely embed the ICanHaz.js template at <ICANHAZ_DIRS-entry>/templatename.html into your Django template, automatically wrapped in <script id="templatename" type="text/html">, ready for ich.templatename({...}) in your JavaScript.

django-icanhaz does not bundle ICanHaz.js or provide any JavaScript utilities; it just helps you easily embed the templates in your HTML. Include ICanHaz.js in your project's static assets and use it in your JS as usual.

Advanced usage

You can also bundle ICanHaz templates with Django reusable apps; by default django-icanhaz will look for templates in a jstemplates subdirectory of each app in INSTALLED_APPS. The app subdirectory name(s) to check can be configured via the ICANHAZ_APP_DIRNAMES setting, which defaults to ["jstemplates"].

The finding of templates can be fully controlled via the ICANHAZ_FINDERS setting, which is a list of dotted paths to finder classes. A finder class should be instantiable with no arguments, and have a find(name) method which returns the full absolute path to a template file, given a base-name.

By default, ICANHAZ_FINDERS contains "icanhaz.finders.FilesystemFinder" (which searches directories listed in ICANHAZ_DIRS) and "icanhaz.finders.AppFinder" (which searches subdirectories named in ICANHAZ_APP_DIRNAMES of each app in INSTALLED_APPS), in that order -- thus templates found in ICANHAZ_DIRS take precedence over templates in apps.

Rationale

The collision between Django templates' use of {{ and }} as template variable markers and ICanHaz.js' use of same has spawned a variety of solutions. One solution simply replaces [[ and ]] with {{ and }} inside an icanhaz 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-icanhaz's People

Contributors

brad avatar carljm avatar d-ross 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

Watchers

 avatar  avatar  avatar

django-icanhaz's Issues

i18n not possible with this solution?

do I understand this correctly? With this solution it would not be possible to have {% trans "foo" %} tags in the icanhaz templates since those are no longer being parsed by django?

how do you handle i18n in your projects when using django-icanhaz?

Ensure app template dirs are always absolute paths

If an app is imported via a relative entry on sys.path, then os.path.dirname(mod.__file__) could return a relative path, which would result in a relative app template dir path. This causes problems with the startswith sanity check in FilesystemFinder.find. The fix is to add an abspath in _get_app_template_dirs to ensure that it always returns full absolute paths.

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.