I got this far:
>>> some_template = get_template_from_string(
... load_template_source(
... 'some_template.html',
... settings.TEMPLATE_DIRS))
...
>>> blocks = some_template.nodelist.get_nodes_by_type(BlockNode)
>>> blocks[0]
<Block Node: another_block. Contents: [<Text Node: '\nThis one is really cool'>, <Block Node: sub_block. Contents: [<Text Node: '\nI\'m a sub-block.\n\t'>]>, <Text Node: '\n'>]>
>>> # Right there is when I realized this wasn't going to be fun.
You see, the contents of a block are contained in block.nodelist
, as opposed to just plain text. If I have a template:
{% extends "base.html" %}
{% block some_block %}
Some value
{% endblock %}
{% block other_block %}
Other Value
{% sub_block %}Sub block value{% endblock %}
{% endblock %}
I want to be able to do this:
>>> get_block_source('other_block')
'\nOther Value\n {% sub_block %}Sub block value{% endblock %}\n'
>>> get_block_source('sub_block')
'Sub block value'
If Django's internals don't provide enough resourced to find a way to do this, I'm OK with using a regex / series of regex as well, but I don't see how it'd be possible with regex alone, given that you can have nested {% block...
tags.