public
Last active

Django tag for ICanHaz.js templates

  • Download Gist
example_template.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14
 
{# ICanHaz templates #}
{% icanhaz %}
 
{# inside here all occurrences of "[[" will become "{{" and "]]" will be "}}" #}
 
<script type="text/html" id="contributor_row">
<select name=role_for_[[ id ]]">
{% for role in roles %}
<option value="{{ role.id }}">{{ role.name }}</option>
{% endfor %}
</select>
<script>
{% endicanhaz %}
icanhaz.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
from django import template
 
register = template.Library()
 
 
@register.tag
def icanhaz(parser, token):
nodelist = parser.parse(('endicanhaz',))
parser.delete_first_token()
return ICanHazNode(nodelist)
 
 
class ICanHazNode(template.Node):
def __init__(self, nodelist):
self.nodelist = nodelist
 
def render(self, context, ):
output = self.nodelist.render(context)
return output.replace('[[', '{{').replace(']]', '}}')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.