Is it possible to extend a base template with another template in Smarty?
I know this is pos开发者_运维知识库sible in Django using the {% entend %} tag. Is there an equivalent (or workaround) in Smarty?Thanks
Although this question is a bit old, I thought that maybe someone looking for this information as of august 2011 would benefit to know that this can be done now with Smarty 3.
Example With Inheritance
layout.tpl
<html>
<head>
<title>{block name=title}Default Page Title{/block}</title>
</head>
<body>
{block name=body}{/block}
</body>
</html>
mypage.tpl
{extends file="layout.tpl"}
{block name=title}My Page Title{/block}
{block name=body}My HTML Page Body goes here{/block}
output of mypage.tpl
<html>
<head>
<title>My Page Title</title>
</head>
<body>
My HTML Page Body goes here
</body>
</html>
Taken verbatim from: http://www.smarty.net/inheritance
There is no build-in template inheritance in Smarty. But you can do similar thing with {include}
and {capture}
.
Your page template can look like:
{capture assign="context"}
<h2>Here is my page</h2>
{... some other smarty suff here ...}
{/capture}
{assign var="title" value="Just simple title text here"}
{include file="base.tpl"}
And base.tpl
can look like following:
<html>
<title>{$title}</title>
<body>
{$context}
</body>
</html>
精彩评论