[Answered ]-How to make a Django template tag which expands into another (macro)

2👍

Create a template filter that will render your custom string instead of a template file.

Declare it like this (this code is tested):

#app_name/templatetags/frobnicative_tags.py
from django.template import Template, Context
register = Library()

@register.tag(name = 'frobnicate2')
def frobnicate2(parser, token):
    args = token.split_contents()
    template_string = """
       {%% load my-third-party-frobnicator-lib %%}
       {%% frobnicate %s %%}
          do stuff with {{ frobnicator }}
       {%% endfrobnicate %%}
    """   
    return Frobnicate2(''.join(args[1:]), template_string)

class Frobnicate2(template.Node):
    def __init__(self, frobnicative_args, template_string):
        self.template_string = template_string
        self.args = frobnicative_args

    def render(self, context):
        """
        Dict inside a Context() is empty because we
        need to pass a context in order to render, even if
        the context is empty.
        """   
        return Template(self.template_string % self.args).render(Context({}))

Don’t forget to replace “my-third-party-frobnicator-lib” with a name of an actual third party library that you’re loading.

Then you can render the whole whird-party tag sequence using {% frobnicate2 "foo", "bar", "baz" %} just as you wanted.

Leave a comment