Created
February 26, 2015 07:22
-
-
Save marucc/d8dadd06cb3ccf5970b2 to your computer and use it in GitHub Desktop.
Djangoのテンプレートシンタックスとかち合わないようにjQuery Templatesを使った場合に作ったテンプレートプラグイン。
templatetags以下に置く。
Djangoのテンプレートhtml内で {% load jq_tmpl %} して使う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from django import template | |
register = template.Library() | |
class JqTagNode(template.Node): | |
def __init__(self, tag_name, arg): | |
self.tag_name = tag_name | |
self.arg = arg | |
def render(self, context): | |
text = [] | |
text.append("{{") | |
text.append(self.tag_name) | |
if self.arg: | |
text.append(" ") | |
text.append(self.arg) | |
text.append("}}") | |
return ''.join(text) | |
@register.tag | |
def jq_if(parser, token): | |
""" | |
ex) | |
{% jq_if VALUE %} | |
{% endjq_if %} | |
""" | |
try: | |
tmp, arg = token.contents.split(None, 1) | |
except ValueError: | |
raise template.TemplateSyntaxError, "%r tag requires arguments" % token.contents.split()[0] | |
return JqTagNode("if", arg) | |
@register.tag | |
def jq_else(parser, token): | |
""" | |
ex) | |
{% jq_if VALUE1 %} | |
{% jq_else VALUE2 %} | |
{% jq_else %} | |
{% endjq_if %} | |
""" | |
try: | |
tmp, arg = token.contents.split(None, 1) | |
except ValueError: | |
arg = '' | |
return JqTagNode("else", arg) | |
@register.tag | |
def endjq_if(parser, token): | |
""" | |
ex) | |
{% jq_if VALUE %} | |
{% endjq_if %} | |
""" | |
try: | |
tmp, arg = token.contents.split(None, 1) | |
except ValueError: | |
arg = '' | |
return JqTagNode("/if", arg) | |
@register.tag | |
def jq_each(parser, token): | |
""" | |
ex) | |
{% jq_each VALUE %} | |
{% endjq_each %} | |
""" | |
try: | |
tmp, arg = token.contents.split(None, 1) | |
except ValueError: | |
raise template.TemplateSyntaxError, "%r tag requires arguments" % token.contents.split()[0] | |
return JqTagNode("each", arg) | |
@register.tag | |
def endjq_each(parser, token): | |
""" | |
ex) | |
{% jq_each VALUE %} | |
{% endjq_each %} | |
""" | |
try: | |
tmp, arg = token.contents.split(None, 1) | |
except ValueError: | |
arg = '' | |
return JqTagNode("/each", arg) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment