View previous topic :: View next topic |
Author |
Message |
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Thu Mar 26, 2009 1:42 pm Post subject: template inheritance |
|
|
from another thread
mohrt wrote: | Smarty 3 will have template inheritance. Example:
base.tpl:
Code: | <html>
<head>
<title>{block name="title"}default{/block}</title>
{block name="head"}{/block}
</head>
<body>
{block name="body"}{/block}
</body>
</html> |
my.tpl:
Code: | {extend file="base.tpl"}
{block name="title"} my title {/block}
{block name="head"} <!-- insert js/css here --> {/block}
{block name="body"} <!-- insert content here --> {/block} |
output of $smarty->display('my.tpl'):
Code: | <html>
<head>
<title>my title</title>
<!-- insert js/css here -->
</head>
<body>
<!-- insert content here -->
</body>
</html> |
|
Forgive me if this is something I just missed in the docs or a forum topic.
I was thinking about the case of a controller with a selectable theme. I think it would good if the extended file did not have to have a {extend file="base.tpl"} to say what file it's extending, because that should be up to the controller. Maybe the inheritance chain could be specified in the PHP. For example, the PHP for the above could be
$smarty->extends('base.tpl');
$smarty->display('my.tpl');
I'm not sure how cases of A extends B, extends C could be handled, maybe pass an array to extends() or extends becomes a variable-length parameter list. Also, I am not sure how it would be handled if the PHP says the template extends one file and the template says another. Maybe the PHP takes precedence.
Another thing, with
{block name="body"} <!-- insert content here --> {/block}
will we have the option of
1) replacing what's already in that block
2) appending to what exists
3) prepending to what exists
I'm thinking this may be useful in some cases. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 26, 2009 2:07 pm Post subject: |
|
|
template inheritance is part of the template render process, no different than an {include}. It could be very confusing to debug a template that is inheriting and you don't see that from the template at first.
As for getting the parent block content, I think you will be able to but I don't know if we've landed on a good syntax for it yet. |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Thu Mar 26, 2009 2:30 pm Post subject: |
|
|
mohrt wrote: | template inheritance is part of the template render process, no different than an {include}. It could be very confusing to debug a template that is inheriting and you don't see that from the template at first.
As for getting the parent block content, I think you will be able to but I don't know if we've landed on a good syntax for it yet. |
Too bad, this syntax seems perfect for a selectable theme type setup where the controller has control over what theme is shown. Any suggestions on how to do that using extension?
Most of the smarty I work with is not for the entire page. The controller takes it and adds it to the theme. Yet, I don't find it confusing, because I am aware that the controller is doing what it is supposed to be doing. Debugging a smarty template, inheritance or not, will be confusing to any one if they don't know how the controller works. So, IMO, that's not the issue.
The only issue to me is that it could complicate things having extend in the template and in the PHP.
As an alternative to "getting" the parent block content, you could just go with:
{block name="body" where="prepend"}
I'm not sure how much processing I could do with the parent block content in Smarty, even if I was able to get it. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 26, 2009 6:25 pm Post subject: |
|
|
We are brainstorming this. It may be possible to handle via a resource:
Code: | $smarty->display('extend:base.tpl|foo.tpl'); |
Then you can chain them together, and use in an {include} tag. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 26, 2009 7:12 pm Post subject: |
|
|
It looks like we currently us {$smarty.block.foo} to get the parent block content, where foo is the name of the block. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Thu Mar 26, 2009 9:48 pm Post subject: |
|
|
It has been changed now to
{$smarty.block.foo.parent} |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Fri Mar 27, 2009 12:46 am Post subject: |
|
|
U.Tews wrote: | It has been changed now to
{$smarty.block.foo.parent} |
So, correct me if I'm wrong, to add to what's already in the block, you can do:
{block name="body"}
{$smarty.block.body.parent}
<!-- more stuff here -->
{/block}
Another thing I was thinking of:
if you extend another file, and you have html outside of the block declarations, will it a) go somewhere in the super-template, b) will it disappear, or c) will that be not allowed?
Would it be useful to have a default block in the super-template where stuff like that could go? For example some how the user could say {block name=body} could be the default place where other HTML could go.
Maybe that wouldn't work, I don't know, but just putting an idea out there. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Mar 27, 2009 1:07 am Post subject: |
|
|
If you extend to another file only {block} tags will be parsed. Everything else is just dropped. If you want any other behaviour you can always put that stuff also into block and can control exactly where it goes. |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Fri Mar 27, 2009 11:56 am Post subject: |
|
|
mohrt wrote: | We are brainstorming this. It may be possible to handle via a resource:
Code: | $smarty->display('extend:base.tpl|foo.tpl'); |
Then you can chain them together, and use in an {include} tag. |
actually now that i've thought about it for a while, I don't think this would work quite the way I was thinking as far as themes go.
The way most of the things I've worked on work, is that the theme is handled by one php file, the content is handled by another, and then some more php code combines the two.
With this method the data for the theme and the content would have to be assigned using the same smarty object, probably with the same php code. so... maybe it was not such a smart idea as i thought... But, I can think of some other things i've worked with that this could be useful for. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Mar 27, 2009 2:22 pm Post subject: |
|
|
Anyway I'm almost done implementing it. As we are still in Alpha we will try to include any usefull feature as long as it does not break the scope of Smarty... |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Mar 27, 2009 3:48 pm Post subject: |
|
|
Template inheritance can now also be called by the new extend resource.
Usage
Code: | Smarty->display('extend:my.tpl|section.tpl|base.tpl');
|
From my.tpl and section.tpl only the {block} tags will we parsed. Should they contain {extend} tags these are ignored. The templates are processed left to right.
It's now in the SVN. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Mar 27, 2009 4:48 pm Post subject: |
|
|
seems backwards, it should be:
Code: | Smarty->display('extend:base.tpl|section.tpl|my.tpl'); |
Where my.tpl is the last on the inheritance chain. |
|
Back to top |
|
U.Tews Administrator
Joined: 22 Nov 2006 Posts: 5068 Location: Hamburg / Germany
|
Posted: Fri Mar 27, 2009 5:37 pm Post subject: |
|
|
Why?
my.tpl is the first one which must be processed. Then we go further down the chain until we get to the final output page. Compiled and cache file names are based on my.tpl.
Same thing as if a template files uses the {extend} tag. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Mar 27, 2009 6:39 pm Post subject: |
|
|
hmm yeah I see where it makes sense that way. From in the template you must go backwards with {extend}. Just looking at it though, it seems that the left side would be the root, and the right side would be the result. |
|
Back to top |
|
douglassdavis Smarty Junkie
Joined: 21 Jan 2008 Posts: 541
|
Posted: Fri Mar 27, 2009 6:46 pm Post subject: |
|
|
honestly as a user, i think the order that you process it in is an implementation detail that most people will not be concerned with.
To me, from the perspective that the super-template must exist before the sub-template, and the perspective that people usually list nodes starting from the root first, this makes more sense:
Code: |
Smarty->display('extend:base.tpl|section.tpl|my.tpl');
|
just one mans opinion |
|
Back to top |
|
|