dldevinc/jinja2-simple-tags

Question: ContainerTag implementation

Closed this issue · 1 comments

When using ContainerTag, how to access tag contents?
I'd like to do something like this:

{% mytag "myarg" %}

this is some text

{% endmytag %}

I can access tag arguments, but can't figure out how to get to the tag's content ("this is some text") .

You can get the inner content by accessing the caller argument. The caller argument will be a Callable object which will return a string of the content inside the tag. For example:

class CustomTagExtension(ContainerTag):
    tags = {"mytag"}

    def render(self, tag_name="p", caller=None):
        return "<{tag}>{content}</{tag}>".format(
            tag=tag_name,
            content=caller()
        )

Usage:

{% mytag "h1" %}Hello, world!{% endmytag %}