Skip to content

Instantly share code, notes, and snippets.

@SAPikachu
Forked from skyl/kwacros.py
Last active December 14, 2015 13:59
Show Gist options
  • Save SAPikachu/5097591 to your computer and use it in GitHub Desktop.
Save SAPikachu/5097591 to your computer and use it in GitHub Desktop.
Make {% loadkwacros %} be able to use variables and filters
#
# templatetags/kwacros.py - Support for macros in Django templates
#
# Based on snippet by
# Author: Michal Ludvig <michal@logix.cz>
# http://www.logix.cz/michal
#
# modified for args and kwargs by Skylar Saveland http://skyl.org
#
"""
Usage example:
0) Save this file as <yourapp>/templatetags/kwacros.py
1) In your template load the library:
{% load kwacros %}
2) Define a new macro called 'my_macro' that takes two args and a kwarg.
All will be optional.
{% kwacro test2args1kwarg arg1 arg2 baz="Default baz" %}
{% firstof arg1 "default arg1" %}
{% if arg2 %}{{ arg2 }}{% else %}default arg2{% endif %}
{{ baz }}
{% endkwacro %}
3) Use the macro with a string parameters or context variables::
{% usekwacro test2args1kwarg "foo" "bar" baz="KW" %}
<br>
{% usekwacro test2args1kwarg num_pages "bar" %}
<br>
{% usekwacro test2args1kwarg %}
<br>
{% usekwacro test2args1kwarg "new" "foobar"|join:"," baz="diff kwarg" %}
renders like
foo bar KW
77 bar Default baz
default arg1 default arg2 Default baz
new f,o,o,b,a,r diff kwarg
4) Alternatively save your macros in a separate
file, e.g. "mymacros.html" and load it to the
current template with:
{% loadkwacros "mymacros.html" %}
Then use these loaded macros in {% usekwacro %}
as described above.
Bear in mind that defined and loaded kwacros are local
to each template file and are not inherited
through {% extends ... %} tags.
"""
from django import template
from django.template import FilterExpression
from django.template.loader import get_template
register = template.Library()
def _setup_macros_dict(context):
if "_macros" not in context:
context["_macros"] = {}
class DefineMacroNode(template.Node):
def __init__(self, name, nodelist, args):
self.name = name
self.nodelist = nodelist
self.args = []
self.kwargs = {}
for a in args:
if "=" not in a:
self.args.append(a)
else:
name, value = a.split("=")
self.kwargs[name] = value
def render(self, context):
## empty string - {% macro %} tag does no output
return ''
@register.tag(name="kwacro")
def do_macro(parser, token):
try:
args = token.split_contents()
tag_name, macro_name, args = args[0], args[1], args[2:]
except IndexError:
m = ("'%s' tag requires at least one argument (macro name)"
% token.contents.split()[0])
raise template.TemplateSyntaxError, m
# TODO: could do some validations here,
# for now, "blow your head clean off"
nodelist = parser.parse(('endkwacro', ))
parser.delete_first_token()
node = DefineMacroNode(macro_name, nodelist, args)
parser._macros = getattr(parser, "_macros", {})
parser._macros[macro_name] = node
return node
class LoadMacrosNode(template.Node):
def __init__(self, file_name_expr):
self.file_name_expr = file_name_expr
self.loaded = False
def render(self, context):
if not self.loaded:
_setup_macros_dict(context)
context["_macros"].update(self.load_macros(context))
## empty string - {% loadmacros %} tag does no output
return ''
def load_macros(self, context, ignore_failures=False):
try:
file_name = self.file_name_expr.resolve(
context,
ignore_failures=True,
)
except template.VariableDoesNotExist:
file_name = None
if file_name is None:
if ignore_failures:
return None
raise template.TemplateSyntaxError("Unable to resolve file name: " + str(self.file_name_expr))
t = get_template(file_name)
macros = t.nodelist.get_nodes_by_type(DefineMacroNode)
return {x.name: x for x in macros}
@register.tag(name="loadkwacros")
def do_loadmacros(parser, token):
try:
tag_name, filename = token.split_contents()
except IndexError:
m = ("'%s' tag requires at least one argument (macro name)"
% token.contents.split()[0])
raise template.TemplateSyntaxError, m
node = LoadMacrosNode(parser.compile_filter(filename))
preloaded_macros = node.load_macros({}, ignore_failures=True)
if preloaded_macros:
parser._macros = getattr(parser, "_macros", {})
parser._macros.update(preloaded_macros)
node.loaded = True
return node
class UseMacroNode(template.Node):
def __init__(self, macro_name, parser, fe_args, fe_kwargs):
self.macro_name = macro_name
self.parser = parser
self.fe_args = fe_args
self.fe_kwargs = fe_kwargs
def render(self, context):
try:
context_macros = context.get("_macros", {})
if self.macro_name in context_macros:
macro = context_macros[self.macro_name]
else:
macro = self.parser._macros[self.macro_name]
except (AttributeError, KeyError):
m = "Macro '%s' is not defined" % self.macro_name
raise template.TemplateSyntaxError, m
for i, arg in enumerate(macro.args):
try:
fe = self.fe_args[i]
context[arg] = fe.resolve(context)
except IndexError:
context[arg] = ""
for name, default in macro.kwargs.iteritems():
if name in self.fe_kwargs:
context[name] = self.fe_kwargs[name].resolve(context)
else:
context[name] = FilterExpression(default,
self.parser
).resolve(context)
return macro.nodelist.render(context)
@register.tag(name="usekwacro")
def do_usemacro(parser, token):
try:
args = token.split_contents()
tag_name, macro_name, values = args[0], args[1], args[2:]
except IndexError:
m = ("'%s' tag requires at least one argument (macro name)"
% token.contents.split()[0])
raise template.TemplateSyntaxError, m
fe_kwargs = {}
fe_args = []
for val in values:
if "=" in val:
# kwarg
name, value = val.split("=")
fe_kwargs[name] = FilterExpression(value, parser)
else: # arg
# no validation, go for it ...
fe_args.append(FilterExpression(val, parser))
return UseMacroNode(macro_name, parser, fe_args, fe_kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment