Smarty3 Runtime inheritance? - block / extend

13 views
Skip to first unread message

John Campbell

unread,
Jun 4, 2009, 2:05:11 PM6/4/09
to smarty-d...@googlegroups.com
Hi,

I have been testing Smarty3, and it appears that runtime template
inheritance doesn't work.

For instance if I try:

{extend file=$layout}
Where $layout is one of 'master.tpl', 'master_no_ads.tpl'.

It doesn't really work currently because the compiled template assumes
that the extended file isn't dynamic, so whatever was loaded first is
what is displayed.

I looked at the code, and it looks like runtime inheritance isn't
something that was considered with the current design of block/extend.

There are many nice use cases for runtime inheritance. For instance,
if you two site layouts (e.g. one with ads, and one without ads), then
you could use runtime inheritance to make each page template work. It
also allows the fetching of individual blocks, which is really handy
when using templates for email.

Regards,
John Campbell

U.Tews

unread,
Jun 4, 2009, 4:10:34 PM6/4/09
to Smarty Developers
Dear John

Yes, you are right.

I currently see no easy solution for this problem specially as we can
have any number of extend levels.

The workaround in such case is to set force_compile = true.

Regards Uwe

John Campbell

unread,
Jun 4, 2009, 8:20:37 PM6/4/09
to smarty-d...@googlegroups.com
On Thu, Jun 4, 2009 at 4:10 PM, U.Tews <uwe....@googlemail.com> wrote:
>
> Dear John
>
> Yes, you are right.
>
> I currently see no easy solution for this problem specially as we can
> have any number of extend levels.
>
> The workaround in such case is to set force_compile = true.

Unfortunately that really wont work, as it makes things too slow to
run on a production system.

I may invest the effort to implement this. I have longed for a php
templating system that is feature competitive with Django or Rails
Views.

It seems to me that a compiled block should just store it's content on
a named stack, and, if the template isn't extending another template,
then you should go through the stack and send the output. The extend
command should just add an 'include' at the end of the template.

Does that make sense? The good news is that implemented this way,
there would be no need to do dependency checking at runtime.

Regards,
John Campbell

U.Tews

unread,
Jun 5, 2009, 10:55:45 AM6/5/09
to Smarty Developers
I will rethink how this could be solved.

Monte came up with a better work around for the time being:
You could build a compile_id from the variable extend name.

On Jun 5, 2:20 am, John Campbell <jcampbe...@gmail.com> wrote:

John Campbell

unread,
Jun 5, 2009, 12:07:53 PM6/5/09
to smarty-d...@googlegroups.com
On Fri, Jun 5, 2009 at 10:55 AM, U.Tews <uwe....@googlemail.com> wrote:
>
> I will rethink how this could be solved.
>
> Monte came up with a better work around for the time being:
> You could build a compile_id from the variable extend name.

I spent some time working on it. I got pretty close by using the
extend resource type with:

$tpl->display("extend:$layout|page.tpl");

The only thing I had to change was how the resource name was
generated. It was still a little half baked with regards to
invalidating the compiled template.

Is the extend resource type something that is going to be maintained?
I didn't see any unit tests, and I thought it may be something that is
going to go away.

- - -
Another question, is there a way to access the contents of the parent
block, from the child block. It would be nice to be able to do:

{block name="navigation"} Add something at the top
{$smarty.block.parent} Add something at the bottom{/block}

Thanks for the help.
John Campbell

U.Tews

unread,
Jun 13, 2009, 4:56:14 PM6/13/09
to Smarty Developers
The extend resource does now create unique filenames for compiled
template and cache for each set of template sources. There is no
longer a need to invalidate these files manually or to use a compile
id.

The extend resource will not go away.

On Jun 5, 6:07 pm, John Campbell <jcampbe...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages