我通过本教程在CI安装上实现了智能:http://www.coolphptools.com/codeigniter-smarty
它工作得很好,只是页眉和页脚是通过包含模板中的文件来加载的。
即。
{include file="header.tpl" title="Example Smarty Page" name="$Name"}
...
{include file="footer.tpl"}
有没有办法从控制器或Smarty库类加载它们?
举一个更清楚的例子说明我想要什么;如果没有模板引擎,我只需要通过自定义加载程序来扩展view
方法。
例如。
class MY_Loader extends CI_Loader {
function view( $template, $data = array(), $return = false ) {
$content = parent::view( 'header', $data, $return );
$content .= parent::view( $template, $data, $return );
$content .= parent::view( 'footer', $data, $return );
if( $content )
return $content;
}
}
这一直对我有效,但现在我正在聪明地尝试,我一辈子都想不出如何让它像这样工作。
如果有人能给我指一个正确的方向。那太好了。
PS。很抱歉,如果这个问题以前已经得到了回答,我在谷歌上搜索了两个小时,似乎什么都找不到。我的PHP技能充其量只是中等水平。
我不是专家,但不久前我确实经历过这件事。我所做的是这样的:
header.tpl
<html>
<head>
</head>
<body>
content.tpl
{include file="header.tpl"}
{include file="$content"}
{include file="footer.tpl"}
页脚.tpl
</body>
</html>
然后,您可以总是巧妙地调用content.tpl
,并通过$content
传递实际的正文内容。
不过,正如我所说,我不是专家,所以语法可能不正确,可能有一种更"正确"的方法来做这件事,但我认为这个想法是存在的。
希望能有所帮助。
您应该使用{extends}
语法来利用模板继承。
从您的基本模板文件(我们称之为template.php
)开始。该文件将包括页眉和页脚代码,并指定主要内容的位置(以及您想要指定的任何其他模板代码块,例如旁白)。
(我在这里使用的是非常简单的HTML,只是为了举例。请不要这样编码。)
template.php:
<html>
<head>
<title>{$title|default:'Default Page Title'}</title>
<meta>
<meta>
</head>
<body>
<header>
<h1>My Website</h1>
<nav>
<!-- Navigation -->
</nav>
</header>
<section class="content">
{block name="content"}{/block}
</section>
<footer>
© 2013 My Website
</footer>
</body>
然后,您的各个页面将{extend}
作为主模板文件,并为所有块指定必要的内容。请注意,您可以将块设为可选块,并使用默认值,因此不需要填充所有块(除非您以这种方式进行编码)。
content_page.php:
{extends file="template.php"}
{block name="content"}
<h2>Content Page</h2>
<p>Some text.</p>
{/block}
you can user following way to include smarty tpl view in your controller:
$this->smarty->view('content.tpl')
and header.tpl in content page:
{include file='header.tpl'}