这个类里面比较关键的$main , $vars , function output(),第一个记录了将要输出的模板(其实是一个php文件),第二个以数组方式保存所谓的“模板变量”,第三个是将模板内容输出到浏览器。在我的思路里面,所谓的“模板”不再是单纯的HTML和模板语言结合的文件,而是一个传统的PHP与HTML混合的文件,在模板类向浏览器输出内容的时候,把这个“模板”include进来就可以了,下面就是一个我项目里的“模板”文件:
CODE
<?php include($t.'include/header.php');?>
<?php if ( $_popup ) { ?>
<?=$_pop_window?>
<?php } ?>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td width="479" height="34" background="<?=$_tpl_path?>images/index_05.gif">
<?php if ( $_tpl_cfg_show_search ) { ?>
<?php include($t.'include/search.php');?>
<?php } ?> </td>
<td width="301" background="<?=$_tpl_path?>images/index_05.gif"><IMG SRC="<?=$_tpl_path?>images/index_09.gif" WIDTH=301 HEIGHT=34 ALT=""></td>
</tr>
</tbody>
</table>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
</tbody>
<tr>
<td width="213" height="404" valign="top" background="<?=$_tpl_path?>images/index_20.gif">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><IMG SRC="<?=$_tpl_path?>images/index_13.gif" WIDTH=237 HEIGHT=23 ALT=""></td>
</tr>
</table><?php if ( $_tpl_cfg_show_vote ) { ?>
<table width="237" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="black-12-20">
<tr>
<td width="16%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
<td width="80%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_vote_module_disp_name?></strong></td>
<td width="4%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
</tr>
</table></td>
</tr>
<tr>
<td>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="9"><img src="<?=$_tpl_path?>images/spacer.gif" width="1" height="1"></td>
</tr>
</table>
<?php include($t.$_vote_module_file);?>
</td>
</tr>
</table>
<?php } ?><?php if ( $_tpl_cfg_show_link ) { ?>
<table width="237" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="black-12-20">
<tr>
<td width="16%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
<td width="80%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_link_module_disp_name?></strong></td>
<td width="4%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
</tr>
</table>
</td>
</tr>
<tr>
<td ><?php include($t.$_link_module_file);?></td>
</tr> </table>
<table width="33%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="66">
<?php if ( $_tpl_cfg_enable_service_module ) { ?>
<div align="center"><a href="javascript:" onClick="MM_openBrWindow('<?=$_common_files?>service/fuwutai.php','','width=531,height=373')"><img src="<?=$_tpl_path?>images/service.gif" width="177" height="49" border='0'></a></div>
<?php } ?>
</td>
</tr>
</table>
<?php } ?>
</td>
<td width="567" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><IMG SRC="<?=$_tpl_path?>images/index_14.gif" WIDTH=543 HEIGHT=23 ALT=""></td>
</tr>
</table><?php if ( $_tpl_cfg_show_news ) { ?>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="black-12-20">
<tr>
<td width="2%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
<td width="93%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_news_module_disp_name?></strong></td>
<td width="5%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan='2' align='center'>
<?php include($t.$_news_module_file);?>
</td>
</tr> </table> <?php } ?> <?php if ( $_tpl_cfg_show_prod ) { ?>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="black-12-20">
<tr>
<td width="2%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
<td width="93%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_prod_module_disp_name?></strong></td>
<td width="5%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
</tr>
</table></td>
</tr>
<tr>
<td width="100%" height="121" valign="top" colspan='2'>
<?php include($t.$_prod_module_file);?>
</td>
</table> <?php } ?> </td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
<?php include($t.'include/foot.php');?>
</body>
</html>
我从这样的修改中获得了下列好处:
1、效率
根据我的调试,在用Smarty之前,项目首页在我的本本上基本上要消耗0.1x~0.2x秒之间,放弃Smarty以后,基本稳定在0.07x以下2、更加精简的PHP文件以上就是我通过我的项目对模板技术的反思和修改,请兄弟们PP^^
CODE
<?php include($t.'include/header.php');?>
<?php if ( $_popup ) { ?>
<?=$_pop_window?>
<?php } ?>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td width="479" height="34" background="<?=$_tpl_path?>images/index_05.gif">
<?php if ( $_tpl_cfg_show_search ) { ?>
<?php include($t.'include/search.php');?>
<?php } ?> </td>
<td width="301" background="<?=$_tpl_path?>images/index_05.gif"><IMG SRC="<?=$_tpl_path?>images/index_09.gif" WIDTH=301 HEIGHT=34 ALT=""></td>
</tr>
</tbody>
</table>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
</tbody>
<tr>
<td width="213" height="404" valign="top" background="<?=$_tpl_path?>images/index_20.gif">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><IMG SRC="<?=$_tpl_path?>images/index_13.gif" WIDTH=237 HEIGHT=23 ALT=""></td>
</tr>
</table><?php if ( $_tpl_cfg_show_vote ) { ?>
<table width="237" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><table width="100%" border="0" cellpadding="0" cellspacing="0" class="black-12-20">
<tr>
<td width="16%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
<td width="80%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_vote_module_disp_name?></strong></td>
<td width="4%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
</tr>
</table></td>
</tr>
<tr>
<td>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="9"><img src="<?=$_tpl_path?>images/spacer.gif" width="1" height="1"></td>
</tr>
</table>
<?php include($t.$_vote_module_file);?>
</td>
</tr>
</table>
<?php } ?><?php if ( $_tpl_cfg_show_link ) { ?>
<table width="237" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="black-12-20">
<tr>
<td width="16%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
<td width="80%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_link_module_disp_name?></strong></td>
<td width="4%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
</tr>
</table>
</td>
</tr>
<tr>
<td ><?php include($t.$_link_module_file);?></td>
</tr> </table>
<table width="33%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="66">
<?php if ( $_tpl_cfg_enable_service_module ) { ?>
<div align="center"><a href="javascript:" onClick="MM_openBrWindow('<?=$_common_files?>service/fuwutai.php','','width=531,height=373')"><img src="<?=$_tpl_path?>images/service.gif" width="177" height="49" border='0'></a></div>
<?php } ?>
</td>
</tr>
</table>
<?php } ?>
</td>
<td width="567" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><IMG SRC="<?=$_tpl_path?>images/index_14.gif" WIDTH=543 HEIGHT=23 ALT=""></td>
</tr>
</table><?php if ( $_tpl_cfg_show_news ) { ?>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="black-12-20">
<tr>
<td width="2%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
<td width="93%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_news_module_disp_name?></strong></td>
<td width="5%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan='2' align='center'>
<?php include($t.$_news_module_file);?>
</td>
</tr> </table> <?php } ?> <?php if ( $_tpl_cfg_show_prod ) { ?>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td colspan="2"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="black-12-20">
<tr>
<td width="2%"><img src="<?=$_tpl_path?>images/index_15.gif" width=37 height=33 alt=""></td>
<td width="93%" background="<?=$_tpl_path?>images/index_16.gif"><strong><?=$_prod_module_disp_name?></strong></td>
<td width="5%"><IMG SRC="<?=$_tpl_path?>images/index_18.gif" WIDTH=29 HEIGHT=33 ALT=""></td>
</tr>
</table></td>
</tr>
<tr>
<td width="100%" height="121" valign="top" colspan='2'>
<?php include($t.$_prod_module_file);?>
</td>
</table> <?php } ?> </td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
<?php include($t.'include/foot.php');?>
</body>
</html>
我从这样的修改中获得了下列好处:
1、效率
根据我的调试,在用Smarty之前,项目首页在我的本本上基本上要消耗0.1x~0.2x秒之间,放弃Smarty以后,基本稳定在0.07x以下2、更加精简的PHP文件以上就是我通过我的项目对模板技术的反思和修改,请兄弟们PP^^
phplib是不是比Smarty小一些?不是“庞然大物”
<?php if ( $_tpl_cfg_show_search ) { ?>
<?php include($t.'include/search.php');?>
<?php } ?>
肯定没有
<IfConfigShowSearch>
<Include Filename="include/search.php">
</IfConfigShowSearch>
这样的模板语言简练好看,还能定义成XML格式的,这就是Smarty的扩展性很好。
比如字符串的输出,可以有很多格式:
如果需要全部大小输出:按照你的逻辑应该是: <?= $var ?>
这样所有需要大写输出的地方你都得加: <?php $var = touppercase($var);?> 那什么时候需要输出什么时候不需要输出都是你规定的,美工怎么知道?
而用Smarty这些都可以全部统一的:{$var upppercase="true"}
还有日期格式的输出:{$var format="xxxx年xx月xx日"} {$var format="xxxx-xx-xx"}
还有加密方式: {$var encode="base64" upppercase="true" attribute="password"}用你的模板技术能做到吗?因为你的设计思想里压根就没这一点。你最多规定一种格式,要另一种格式输出你又得改源码,还有各种组合格式,一来一去开发效率就不高,这时候你会觉得Smarty设计得很巧妙和实用。
凡事都有它的适用范围,Smarty有它存在的理由,我想我只是换了一种思路来考虑我项目中的“模板”这个问题,我并没有发明一种我自己的所谓“模板”。更让我困惑的是,为什么非得要php文件使用一种语言(php本身),模板又要换一种语言,Smarty既然可以以那种统一的格式来处理变量,难道PHP语言本身就不可以?
呵呵~~~~~~~~
关于Smarty的东西
可以是在标题之后加上一个有特殊标记的图片....总之由你决定.这一点是很爽的.好了.罗索了这么多,各位高人有什么感想说出来听听.
用SMARTY来做小型网站简直就是“杀鸡用牛刀”!
其实最好根据你自己的项目来选择模板引擎,小型的可以使用PHPLIB,中大型的那就选择SMARTTEMPLATE或者SMARTY。
我比较喜欢SMARTTEMPLATE的哈,小又快
http://phpsavant.com/yawiki/index.php?area=Savant2&page=StartExample见下面的例子:
Model Logic
<?php
require_once 'Savant2.php';
$tpl =& new Savant2();// Create a title.
$name = "Some Of My Favorite Books";// Generate an array of book authors and titles.
$booklist = array(
array(
'author' => 'Hernando de Soto',
'title' => 'The Mystery of Capitalism'
),
array(
'author' => 'Milton Friedman',
'title' => 'Free to Choose'
)
);// Assign values to the Savant instance.
$tpl->assign('title', $name);
$tpl->assign('books', $booklist);// Display a template using the assigned values.
$tpl->display('books.tpl.php');
?>View Logic <?php if (is_array($this->books)): ?>
<!-- A table of some books. -->
<table>
<tr>
<th>Author</th>
<th>Title</th>
</tr>
<?php foreach ($this->books as $key => $val): ?>
<tr>
<td><?php echo $val['author'] ?></td>
<td><?php echo $val['title'] ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php else: ?>
<p>There are no books to display.</p>
<?php endif; ?>
这个开发大型网站是没一点问题的,为什么呢?我已经实践过了
曾经开启过它的缓存功能,但用在session实时更新时,不得不关闭它,然后用
它的cache_handler将它存在数据库中,但速度并没有改变多少。
不知道是不是自己没有用好的缘故,大家觉得呢?