[PHP] - Smarty母版使用方法(类似ASP.NET母版)

来源:http://www.flyy.info/480.html

用过asp.net的母版页的人应该感觉这个东西还是有值得php借鉴的地方。
新出来的 smarty 3  引入了 Template Inheritance 概念,能和asp.net 的母版页实现同样的功能。

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>

posted @ 2011-06-05 14:16  炎峰森林影  阅读(578)  评论(0编辑  收藏  举报