用过asp.net的母版页的人应该感觉这个东西还是有值得php借鉴的地方。
新出来的 smarty 3 引入了 Template Inheritance 概念,能和asp.net 的母版页实现同样的功能。
Example 15.6. Template inheritance example
layout.tpl (parent)
<html> <head> <title>{block name=title}Default Page Title{/block}</title> {block name=head}{/block} </head> <body> {block name=body}{/block} </body> </html>
myproject.tpl (child)
{extends file='layout.tpl'} {block name=head} <link href="/css/mypage.css" rel="stylesheet" type="text/css"/> <script src="/js/mypage.js"></script> {/block}
mypage.tpl (grandchild)
{extends file='myproject.tpl'} {block name=title}My Page Title{/block} {block name=head} <link href="/css/mypage.css" rel="stylesheet" type="text/css"/> <script src="/js/mypage.js"></script> {/block} {block name=body}My HTML Page Body goes here{/block}
To render the above use
$smarty->display('mypage.tpl');
The resulting output is
<html> <head> <title>My Page Title</title> <link href="/css/mypage.css" rel="stylesheet" type="text/css"/> <script src="/js/mypage.js"></script> </head> <body> My HTML Page Body goes here </body> </html>