Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created October 7, 2010 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jboesch/614548 to your computer and use it in GitHub Desktop.
Save jboesch/614548 to your computer and use it in GitHub Desktop.
django workaround for jquery tmpl tags
<script id="person-tmpl-django" class="fix" type="text/x-jquery-tmpl">
<div id="person-${user.id}" class="person">
<strong>${user.name}</strong>
[[each(i, c) user.children]]
<div class="child">${c.child_name}</div>
[[/each]]
<span>${user.age}</span>
</div>
</script>
<script type="text/javascript">
// Since jQuery tmpl uses the same tags as django {{ }}, we need to code it
// differently (square brackets) then run the replacements client-side to
// change it back so django won't pick it up.
$(function(){
$("script.fix").each(function(){
$(this).text($(this).text().replace(/\[\[/g, "{{").replace(/\]\]/g, "}}"));
});
});
// Make ajax request
$.post('json/add_person.json', { person: 'bob', age: 55 }, function(data){
$.tmpl($('#person-tmpl-django'), data).appendTo('#people');
}, 'json');
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment