I want to use AngularJS with Django however they both use {{ }} as their template tags. Is there an easy way to change one of the two to use some other custom templating tag?

Solution 1

For Angular 1.0 you should use the $interpolateProvider apis to configure the interpolation symbols:$interpolateProvider.

Something like this should do the trick:

myModule.config(function($interpolateProvider) {

Keep in mind two things:

  • mixing server-side and client-side templates is rarely a good idea and should be used with caution. The main issues are: maintainability (hard to read) and security (double interpolation could expose a new security vector - e.g. while escaping of serverside and clientside templating by themselves might be secure, their combination might not be).
  • if you start using third-party directives (components) that use {{ }} in their templates then your configuration will break them. (fix pending)

While there is nothing we can do about the first issue, except for warning people, we do need to address the second issue.

Solution 2

you can maybe try verbatim Django template tag and use it like this :

<script src=""></script>

{% verbatim %}
<div ng-app="">
    <p>10 is {{ 5 + 5 }}</p>
{% endverbatim %}

Solution 3

If you did separate sections of page properly then you can easily use angularjs tags in "raw" tag scope.

In jinja2

{% raw %}
    // here you can write angularjs template tags.
{% endraw %}

In Django template (above 1.5)

{% verbatim %}    
    // here you can write angularjs template tags.
{% endverbatim %}

Solution 4

We created a very simple filter in Django 'ng' that makes it easy to mix the two:


  {{ django_context_var }}
  {{ 'angularScopeVar' | ng }}
  {{ 'angularScopeFunction()' | ng }}

The ng filter looks like this:

from django import template
from django.utils import safestring

register = template.Library()

def Angularify(value):
  return safestring.mark_safe('{{%s}}' % value)

Solution 5

So I got some great help in the Angular IRC channel today. It turns out you can change Angular's template tags very easily. The necessary snippets below should be included after your angular include (the given example appears on their mailing lists and would use (()) as the new template tags, substitute for your own):

angular.markup('(())', function(text, textNode, parentElement){
  if (parentElement[0].nodeName.toLowerCase() == 'script') return;
  text = text.replace(/\(\(/g,'{{').replace(/\)\)/g, '}}');
  return angular.markup('{{}}').call(this, text, textNode, parentElement);

angular.attrMarkup('(())', function(value, name, element){
    value = value.replace(/\(\(/g,'{{').replace(/\)\)/, '}}');
    element[0].setAttribute(name, value);
    return angular.attrMarkup('{{}}').call(this, value, name, element);

Also, I was pointed to an upcoming enhancement that will expose startSymbol and endSymbol properties that can be set to whatever tags you desire.

Solution 6

I vote against using double parentheses (()) as template tag. It may work well as long as no function call is involved but when tried the following


with Firefox (10.0.2) on Mac I got a terribly long error instead of the intended logic. <[]> went well for me, at least up until now.

Edit 2012-03-29: Please note that $invalidWidgets is deprecated. However I'd still use another wrapper than double braces. For any angular version higher than 0.10.7 (I guess) you could change the wrapper a lot easier in your app / module definition:

angular.module('YourAppName', [], function ($interpolateProvider) {

API docs.

Solution 7

I found the code below helpful. I found the code here:


    {% ng Some.angular.scope.content %}

    {% load angularjs %}
    <div ng-init="yourName = 'foobar'">
        <p>{% ng yourName %}</p>

from django import template

register = template.Library()

class AngularJS(template.Node):
    def __init__(self, bits): = bits

    def render(self, ctx):
        return "{{%s}}" % " ".join([1:])

def do_angular(parser, token):
    bits = token.split_contents()
    return AngularJS(bits)

register.tag('ng', do_angular)

Solution 8

You could always use ng-bind instead of {{ }}

<span ng-bind="name"></span>

Solution 9

If you use django 1.5 and newer use:

  {% verbatim %}
    {{if dying}}Still alive.{{/if}}
  {% endverbatim %}

If you are stuck with django 1.2 on appengine extend the django syntax with the verbatim template command like this ...

from django import template

register = template.Library()

class VerbatimNode(template.Node):

    def __init__(self, text):
        self.text = text

    def render(self, context):
        return self.text

def verbatim(parser, token):
    text = []
    while 1:
        token = parser.tokens.pop(0)
        if token.contents == 'endverbatim':
        if token.token_type == template.TOKEN_VAR:
        elif token.token_type == template.TOKEN_BLOCK:
        if token.token_type == template.TOKEN_VAR:
        elif token.token_type == template.TOKEN_BLOCK:
    return VerbatimNode(''.join(text))

In your file use:

from google.appengine.ext.webapp import template


Solution 10

You can tell Django to output {{ and }}, as well as other reserved template strings by using the {% templatetag %} tag.

For instance, using {% templatetag openvariable %} would output {{.

Solution 11

I would stick with a solution that uses both django tags {{}} as well angularjs {{}} with a either a verbatim section or templatetag.

That is simply because you can change the way angularjs works (as mentioned) via the $interpolateProvider.startSymbol $interpolateProvider.endSymbol but if you start to use other angularjs components like the ui-bootstrap you will find that some of the templates are ALREADY built with standard angularjs tags {{ }}.

For example look at

Solution 12

If you do any server-side interpolation, the only correct way to do this is with <>


Anything else is an XSS vector.

This is because any Angular delimiters which are not escaped by Django can be entered by the user into the interpolated string; if someone sets their username as "{{evil_code}}", Angular will happily run it. If you use a character than Django escapes, however, this won't happen.