Skip to content Skip to sidebar Skip to footer

Django - How To Get The Contents Of A {% Block %} Tag From A Template

I got this far: >>> some_template = get_template_from_string( ... load_template_source( ... 'some_template.html', ... settings.TEMPLATE_DIRS)) ... >

Solution 1:

This seems like you're working hard against the Django grain. Put the content into an include file, then {% include %} it in your block, and also read the file directly. If you could tell us more about what you're trying to accomplish, there's probably a better way to do it.

Solution 2:

The solution I created:

import re


BLOCK_RE = re.compile(r'{%\s*block\s*(\w+)\s*%}')
NAMED_BLOCK_RE = r'{%%\s*block\s*%s\s*%%}'# Accepts string formatting
ENDBLOCK_RE = re.compile(r'{%\s*endblock\s*(?:\w+\s*)?%}')


defget_block_source(template_source, block_name):
    """
    Given a template's source code, and the name of a defined block tag,
    returns the source inside the block tag.
    """# Find the open block for the given name
    match = re.search(NAMED_BLOCK_RE % (block_name,), template_source)
    if match isNone:
        raise ValueError(u'Template block {n} not found'.format(n=block_name))
    end = inner_start = start = match.end()
    end_width = 0whileTrue:
        # Set ``end`` current end to just out side the previous end block
        end += end_width
        # Find the next end block
        match = re.search(ENDBLOCK_RE, template_source[end:])
        # Set ``end`` to just inside the next end block
        end += match.start()
        # Get the width of the end block, in case of another iteration
        end_width = match.end() - match.start()
        # Search for any open blocks between any previously found open blocks,# and the current ``end``
        nested = re.search(BLOCK_RE, template_source[inner_start:end])
        if nested isNone:
            # Nothing found, so we have the correct end blockbreakelse:
            # Nested open block found, so set our nested search cursor to just# past the inner open block that was found, and continue iteration
            inner_start += nested.end()
    # Return the value between our ``start`` and final ``end`` locationsreturn template_source[start:end]

Post a Comment for "Django - How To Get The Contents Of A {% Block %} Tag From A Template"