Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6704825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:20:00+00:00 2026-05-26T07:20:00+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:20:01+00:00Added an answer on May 26, 2026 at 7:20 am

    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*)?%}')
    
    
    def get_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 is None:
            raise ValueError(u'Template block {n} not found'.format(n=block_name))
        end = inner_start = start = match.end()
        end_width = 0
        while True:
            # 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 is None:
                # Nothing found, so we have the correct end block
                break
            else:
                # 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`` locations
        return template_source[start:end]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this far: :~ curl -u username:password -d status=new_status http://twitter.com/statuses/update.xml Now, how can
Here's what I got so far (that doesn't work). At this point I thought
I post this previously in Adobe Forum but haven't got any answers so far.
I've got this query so far: if(!empty($_SESSION['s_property_region'])) { $sqlWHERE .= AND $wpdb->postmeta.meta_key = 'property_region'
Got this line of code here but its not working. private void Button_Click(object sender,
Got this site with UN/PW set via the Createuserwizard control. Client considers PW too
Got this: Table a ID RelatedBs 1 NULL 2 NULL Table b AID ID
Got this code for a viewscroller from the apple developers site. @synthesize scrollView1, scrollView2;
I got this error today when trying to open a Visual Studio 2008 project
I got this output when running sudo cpan Scalar::Util::Numeric jmm@freekbox:~/bfwsandbox/sa/angel/astroportal/dtu8e/resources$ sudo cpan Scalar::Util::Numeric [sudo]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.