Replace jquery-templ tag
Is there a way to replace the main tag on jquery-tmpl ?
Example:
var data = {Name: 'Pele', Languages: ["Portuguese","English","Spanish"]};
So on a script tag we define the following template
Name: ${Name} {{each Languages}} I speak $value {{/each}}
What I wanted to change is ... Istead of using {{each}} I'd use something like $$each$$ Instead of ${Name} I'd use something like $#Name$
You may be asking yourself why I wanna do this.
The main reason is when I because on the project we're working on uses Django and when we put code like {{each}} (even on script tag with type set to text/html) Django view engin开发者_开发问答e think it's a server tag and tries to render it like if it were a server side tag.
Update: What I'm looking for is a way to Set a Delimeter on jQuery-tmpl like the one that is avaiable on Mustache.js http://mustache.github.com/mustache.5.html (look for Set Delimiter)
Thanks.
Sure, if you want a literal {
in your HTML, use templatetag
with openblock
.
{% templatetag openblock %}
If you want a literal }
, use closeblock
:
{% templatetag closeblock %}
So if you want {{each}}
in your HTML, use:
{% templatetag openblock %}{% templatetag openblock %}each{% templatetag closeblock %}{% templatetag closeblock %}
A different approach would be to define the template in a js file which is not processed by django as a template.
If that is not possible another alternative to Dominic's approach would be to define variables for '{{' and '}}' maybe jqtmpl_open and jqtmpl_close accordingly and use them in template like this:
{{ jqtmpl_open }}each Languages{{ jqtmpl_close }}
This would be more readable in the template.
Changing a delimiter for jquery-tmpl is complicated. By looking at the code it seems that {{ is hard coded within some regular expressions there.
var oldManip = jQuery.fn.domManip, tmplItmAtt = "_tmplitem", htmlExpr = /^[^<]* (<[\w\W]+>)[^>]*$|\{\{\! /,
newTmplItems = {}, wrappedItems = {}, appendToTmplItems, topTmplItem = { key: 0, data: {} }, itemKey = 0, cloneIndex = 0, stack = [];
The only solution would be to fork the jquery-tmpl for your project and change these hard coded regular expressions to accommodate your needs.
This question has already been asked here jquery template tags conflict with Django template! but I thought I would add from my experience.
In a nutshell, I've added this custom "raw" template tag to my jquery-tmpl/django projects: http://www.holovaty.com/writing/django-two-phased-rendering/
精彩评论