|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
leanweb Smarty Rookie
Joined: 27 Oct 2004 Posts: 24 Location: Newark, DE
|
Posted: Tue Sep 26, 2006 6:49 am Post subject: yet another template inheritance trick |
|
|
unfortunately i can't post actual code, so please bear with my pseudocode here. the idea should be reasonably clear.
we start with a "root" template, i like to call it the layout template. corresponds to an abstract class in oop parlance:
layout.tpl:
Code: | <html>
<head>
<title>Welcome</title>
</head>
<body>
{$smarty.capture.body}
</body>
</html> |
next we look at an actual webpage template:
page.tpl:
Code: | {capture name="body"}some body text for this page{/capture}
{include file="layout.tpl"} |
in effect {include...} here acts as an equivalent of an oop extend construct and connects inheritance relationship with the parent (here the layout template). its sort of awkward that its at the bottom of template, but this way there's no need for new smarty constructs and probably less overhead.
i've tried nesting up to 3 generations, and dont see why there would be any limit.
i'm yet unsure whether {include} will work here or whether {insert} is more appropriate. {include} does work with caching disabled, i've yet to test it work with caching enabled.
this approach has been successfully used on usabilitybok.org |
|
Back to top |
|
jmun Smarty n00b
Joined: 08 Feb 2008 Posts: 2
|
Posted: Fri Feb 08, 2008 6:43 pm Post subject: |
|
|
taking the same idea. i found a better way to do this so that the parent and child templates can be used interchangeably:
in main.tpl:
Code: |
<html>
<head>
<title>{$title}</title>
</head>
<body>
{$content}
</body>
</html>
|
and in child.tpl:
Code: |
{capture assign=content}{$content}{/capture}
{include file="main.tpl"}
|
and in your logic:
Code: |
$smarty->assign('title',"This is the title");
$smarty->assign('content',"hello world!");
$smarty->display("child.tpl");
or
$smarty->display("main.tpl");
|
you can also replace {$content} in child.tpl with static content |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|