Created
December 19, 2012 04:53
-
-
Save atabary/4334461 to your computer and use it in GitHub Desktop.
Django and angular.js are using a conflicting templating syntax, relying on double curly brackets, eg. `{{ something }}`. This template-tag mitigate the problem by allowing the use of {% ng something %} in a django template, which is then outputed as {{ something }} for angular.js to use.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
filename: angularjs.py | |
Usage: | |
{% ng Some.angular.scope.content %} | |
e.g. | |
{% load angularjs %} | |
<div ng-init="yourName = 'foobar'"> | |
<p>{% ng yourName %}</p> | |
</div> | |
""" | |
from django import template | |
register = template.Library() | |
class AngularJS(template.Node): | |
def __init__(self, bits): | |
self.ng = bits | |
def render(self, ctx): | |
return "{{%s}}" % " ".join(self.ng[1:]) | |
def do_angular(parser, token): | |
bits = token.split_contents() | |
return AngularJS(bits) | |
register.tag('ng', do_angular) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To credit the original author, the code was found here: http://djangosnippets.org/snippets/2787/