Package: django-icanhaz
A templatetag for easier integration ofICanHaz.jsJavaScript templates with
Django templates.QuickstartDependenciesTested withDjango1.3 through trunk, andPython2.6 and 2.7. Almost
certainly works with older versions of both.InstallationInstall from PyPI withpip:pip install django-icanhazor get thein-development version:pip install django-icanhaz==devUsageAdd"icanhaz"to yourINSTALLED_APPSsetting.Set theICANHAZ_DIRSsetting 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.htmlinto your Django template,
automatically wrapped in<scriptid="templatename"type="text/html">,
ready forich.templatename({...})in your JavaScript.django-icanhazdoes not bundleICanHaz.jsor provide any JavaScript
utilities; it just helps you easily embed the templates in your HTML. IncludeICanHaz.jsin your project’s static assets and use it in your JS as usual.Advanced usageYou can also bundle ICanHaz templates with Django reusable apps; by defaultdjango-icanhazwill look for templates in ajstemplatessubdirectory of
each app inINSTALLED_APPS. The app subdirectory name(s) to check can be
configured via theICANHAZ_APP_DIRNAMESsetting, which defaults to["jstemplates"].The finding of templates can be fully controlled via theICANHAZ_FINDERSsetting, which is a list of dotted paths to finder classes. A finder class
should be instantiable with no arguments, and have afind(name)method
which returns the full absolute path to a template file, given a base-name.By default,ICANHAZ_FINDERScontains"icanhaz.finders.FilesystemFinder"(which searches directories listed inICANHAZ_DIRS) and"icanhaz.finders.AppFinder"(which searches subdirectories named inICANHAZ_APP_DIRNAMESof each app inINSTALLED_APPS), in that order –
thus templates found inICANHAZ_DIRStake precedence over templates in
apps.RationaleThe collision between Django templates’ use of{{and}}as template
variable markers andICanHaz.js’ use of same has spawned a variety of
solutions.One solutionsimply replaces[[and]]with{{and}}inside anicanhaztemplate tag;anothermakes 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 thatICanHaz.jsexpects to find it in).Enjoy!CHANGES0.2.1 (2012.01.09)Fixed template reading to explicitly decode template file contents using
Django’sFILE_CHARSETsetting. Thanks Eduard Iskandarov.Fixed template-finding failure with non-normalized directories inICANHAZ_DIRS. Thanks Eduard Iskandarov for report and patch.0.2.0 (2011.06.26)Made template-finding more flexible:ICANHAZ_DIRis nowICANHAZ_DIRS(a list); addedICANHAZ_FINDERS,ICANHAZ_APP_DIRNAMES, and finding of
templates in installed apps.0.1.0 (2011.06.22)Initial release.TODO