Skip to content

Instantly share code, notes, and snippets.

@atabary
Created December 19, 2012 04:53
Show Gist options
  • Save atabary/4334461 to your computer and use it in GitHub Desktop.
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.
"""
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)
@atabary
Copy link
Author

atabary commented Dec 19, 2012

To credit the original author, the code was found here: http://djangosnippets.org/snippets/2787/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment