303π
For Angular 1.0 you should use the $interpolateProvider apis to configure the interpolation symbols: http://docs.angularjs.org/api/ng.$interpolateProvider.
Something like this should do the trick:
myModule.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{[{');
$interpolateProvider.endSymbol('}]}');
});
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.
126π
you can maybe try verbatim Django template tag
and use it like this :
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
{% verbatim %}
<div ng-app="">
<p>10 is {{ 5 + 5 }}</p>
</div>
{% endverbatim %}
- [Django]-Extend base.html problem
- [Django]-How to test auto_now_add in django
- [Django]-Add a custom button to a Django application's admin page
43π
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 %}
- [Django]-How to get the current URL within a Django template?
- [Django]-Cannot set Django to work with smtp.gmail.com
- [Django]-Django queryset filter β Q() | VS __in
31π
We created a very simple filter in Django βngβ that makes it easy to mix the two:
foo.html:
...
<div>
{{ django_context_var }}
{{ 'angularScopeVar' | ng }}
{{ 'angularScopeFunction()' | ng }}
</div>
...
The ng
filter looks like this:
from django import template
from django.utils import safestring
register = template.Library()
@register.filter(name='ng')
def Angularify(value):
return safestring.mark_safe('{{%s}}' % value)
- [Django]-Pulling data to the template from an external database with django
- [Django]-Django count RawQuerySet
- [Django]-Django multiple template inheritance β is this the right style?
27π
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, '}}');
textNode.text(text);
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.
- [Django]-How to force application version on AWS Elastic Beanstalk
- [Django]-Django: guidelines for speeding up template rendering performance
- [Django]-How to recursively query in django efficiently?
18π
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
ng:disabled=(($invalidWidgets.visible()))
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) {
$interpolateProvider.startSymbol('<[');
$interpolateProvider.endSymbol(']>');
});
- [Django]-Django: Fat models and skinny controllers?
- [Django]-Convert Django Model object to dict with all of the fields intact
- [Django]-Django form fails validation on a unique field
17π
You could always use ng-bind instead of {{ }}
http://docs.angularjs.org/api/ng/directive/ngBind
<span ng-bind="name"></span>
- [Django]-Django dynamic forms β on-the-fly field population?
- [Django]-How do I run tests against a Django data migration?
- [Django]-How can I handle Exceptions raised by dango-social-auth?
16π
I found the code below helpful. I found the code here: http://djangosnippets.org/snippets/2787/
"""
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)
- [Django]-How to mix queryset results?
- [Django]-Django.contrib.gis.db.backends.postgis vs django.db.backends.postgresql_psycopg2
- [Django]-DatabaseError: current transaction is aborted, commands ignored until end of transaction block?
12π
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
@register.tag
def verbatim(parser, token):
text = []
while 1:
token = parser.tokens.pop(0)
if token.contents == 'endverbatim':
break
if token.token_type == template.TOKEN_VAR:
text.append('{{')
elif token.token_type == template.TOKEN_BLOCK:
text.append('{%')
text.append(token.contents)
if token.token_type == template.TOKEN_VAR:
text.append('}}')
elif token.token_type == template.TOKEN_BLOCK:
text.append('%}')
return VerbatimNode(''.join(text))
In your file use:
from google.appengine.ext.webapp import template
template.register_template_library('utilities.verbatim_template_tag')
Source:
http://bamboobig.blogspot.co.at/2011/09/notebook-using-jquery-templates-in.html
- [Django]-Django: Multiple forms possible when using FormView?
- [Django]-Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found
- [Django]-What is actually assertEquals in Python?
7π
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 {{
.
- [Django]-Default value for user ForeignKey with Django admin
- [Django]-Serving Media files during deployment in django 1.8
- [Django]-Add additional options to Django form select widget
3π
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 https://github.com/angular-ui/bootstrap/blob/master/template/dialog/message.html.
- [Django]-Separation of business logic and data access in django
- [Django]-Django 2 β How to register a user using email confirmation and CBVs?
- [Django]-Django + Ajax
0π
If you do any server-side interpolation, the only correct way to do this is with <>
$interpolateProvider.startSymbol('<{').endSymbol('}>');
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.
- [Django]-Unittest Django: Mock external API, what is proper way?
- [Django]-Many-To-Many Fields View on Django Admin
- [Django]-Django F() division β How to avoid rounding off