croxton / Stash

Stash allows you to stash text and snippets of code for reuse throughout your templates.
GNU General Public License v3.0
197 stars 20 forks source link

Nesting blocks #54

Closed elivz closed 11 years ago

elivz commented 11 years ago

I may be missing something, but it seems like there is no way to nest blocks. This would be handy when using the template override method to, for instance, be able to override either the entire content section or just the sidebar within it. Is that possible, either now or in the future? Or are there parsing issues that prevent it from working?

Example:

{exp:stash:block name="page_content"}
    <div class="content">
        <div class="body" role="main">
            {exp:stash:block name="page_body"}
                <h1 class="entry-title">{exp:stash:title}</h1>
                {exp:stash:body}
            {/exp:stash:block}
        </div>
        <div class="sidebar">
            {exp:structure:nav start_from="/{segment_1}"}
            {exp:stash:sidebar}
        </div>
    </div>
{/exp:stash:block}
croxton commented 11 years ago

You can indeed nest blocks if you wish, you just need to take account of the limitations of the EE parser. When it parses a tag pair it finds the opening tag and then looks for the closing tag. If you have nested identical tag pairs then the inner closing tag will be found instead of the outermost closing tag.

To get around this limitation you can use a simple trick to stop those opening and closing tags of nested tagpairs being identical to the outer tag pair and thus confusing the parser. Simply append a 4th tagpart to the nested tags which is unique for each nested tag. E.g.:

{exp:stash:block name="page_content"}
    <div class="content">
        <div class="body" role="main">
            {exp:stash:block:nested name="page_body"}
                <h1 class="entry-title">{exp:stash:title}</h1>
                {exp:stash:body}
            {/exp:stash:block:nested}
        </div>
        <div class="sidebar">
            {exp:structure:nav start_from="/{segment_1}"}
            {exp:stash:sidebar}
        </div>
    </div>
{/exp:stash:block}

Using this technique any level of nesting is possible.

elivz commented 11 years ago

Oh, duh. I should have thought to try that. Carry on!

P.S. On the site I am currently developing I am trying using Stash embeds for almost everything, with EE templates basically acting as a routing system. So far, it's been great!