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?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- Django __str__ returned non-string (type NoneType)
- void before promise syntax
- Keeping track of variable instances
If you did separate sections of page properly then you can easily use angularjs tags in "raw" tag scope.
In jinja2
In Django template (above 1.5)
I found the code below helpful. I found the code here: http://djangosnippets.org/snippets/2787/
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.
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.
If you use django 1.5 and newer use:
If you are stuck with django 1.2 on appengine extend the django syntax with the verbatim template command like this ...
In your file use:
Source: http://bamboobig.blogspot.co.at/2011/09/notebook-using-jquery-templates-in.html
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):Also, I was pointed to an upcoming enhancement that will expose
startSymbol
andendSymbol
properties that can be set to whatever tags you desire.