Page 1 of 1

[SOLVED] Smarty Template Inheritance and Defined ContenT

Posted: Thu Dec 06, 2012 11:16 am
by lewishowles
It appears that using when using the new Smarty template inheritance idea, some odd things happen. It seems that even if you replace the contents of a block, it's still being parsed. I have this in my main template:

Code: Select all

{block name='content_area'}
   <div class="header clearing">
      {content assign='subtitle' block='subtitle' label='Page Subtitle'}
      <h2>{$subtitle}</h2>
      {content assign='pagelink' block='pagelink' label='Page Link'}
      {content assign='pagelinktext' block='pagelinktext' label='Page Link Text'}
      {if $pagelink != '' && $pagelinktext != ''}
         <a href="{cms_selflink href=$pagelink}" class="button more">{$pagelinktext}</a>
      {/if}
   </div>

   <div class="wrapper">
      {$main_content}

      {if $pagelink != '' && $pagelinktext != ''}
         <a href="{cms_selflink href=$pagelink}" class="button mobile">{$pagelinktext}</a>
      {/if}
   </div>
{/block}
Then in a secondary template I have this:

Code: Select all

{extends file='template:Default'}

{block name='content_area'}
    {$main_content}
{/block}

I've had to assign {content} to {$main_content} because it was complaining about duplicating the main content block, which is what makes me think it parses it even if you replace the contents of the block in the sub-template.

This isn't too bad for just the content block as assigning it isn't very difficult - but it means all of the content blocks defined in the original template (which I don't want in the sub template) still appear on the sub template page.

Is this the expected behaviour or am I missing something? Is there another way to do it without creating a separate template?

Or is the easiest way to have a master template that has neither set of content, then two templates that extend it - one for the set of content blocks and one for the simplified content?

Re: Smarty Template Inheritance and Defined Content Blocks

Posted: Thu Dec 06, 2012 11:45 pm
by fredp
Hi,

I suspect that you're seeing caching effects. You might want to review the CMSMS and Smarty cache related docs (e.g, CMSMS caching and Smarty cacheability).

In particular...
Special notes for Site developers

Due to limitations in Smarty3, when caching for a page is enabled, special consideration must be used to capture the output of a non-cached plugin. In order to capture the output of a plugin that does not cache, you need to use either the "capture" smarty compiler tag, or the "nocache" tag attribute. i.e:

{capture assign='mycontent'}{content}{/capture} or {content assign='mycontent'}{$mycontent nocache}
You could try adding a "nocache" option to your {$main_content} tag in your secondary template to see if that gets you closer to your desired results:
{$main_content nocache}
Hope this helps,
fredp

Re: Smarty Template Inheritance and Defined Content Blocks

Posted: Fri Dec 07, 2012 12:30 am
by calguy1000
The {content} tag is a compiler tag which means yes, it gets parsed in sub templates.

If you want to have child templates that have extra content blocks that should be fine. However, you cannot remove, or hide content blocks in child templates.

Re: Smarty Template Inheritance and Defined Content Blocks

Posted: Wed Dec 12, 2012 1:13 pm
by lewishowles
That's what I was suspecting. That's a shame, but it's not too big a deal to have one master and two different sub for the different sets of content blocks. Thanks!