现在的位置: 首页 > 综合 > 正文

smarty 文件嵌套调用

2013年08月08日 ⁄ 综合 ⁄ 共 2771字 ⁄ 字号 评论关闭

解决了困扰一天的字符验证的问题后,在csdn闲逛,发现一篇smarty嵌套调用的问题
G了几篇文章后分析了一下.

先假设有四个文件,php文件在同一目录下,名称相同的互相匹配的为(php+模板)
index.php
index.tpl
header.php
header.tpl

嵌套结构一
index.php
    │
    └─index.tpl
          │
          └─header.php
                │
                └─header.tpl

嵌套结构的好处:header的部分与index没有什么关联,可以方便的修改特性,代码如下:

index.php
<?php
/*模板设置*/
require_once($_SERVER["DOCUMENT_ROOT"].’/Smarty/Smarty.class.php’);
$smarty = new Smarty();

$smarty->template_dir = ‘./include/templates’;
$smarty->compile_dir = ‘./include/templates_c’;
$smarty->cache_dir = ‘./include/cache’;
$smarty->config_dir = ‘./include/config’;
/**********************************************************/
$index = “主页”;
$smarty->assign(’index’,$index);
$smarty->display(index.tpl);
?>

index.tpl
<html>
{include_php file=”./header.php”}
<body>{$index}</body>
</html>

header.php
<?php
$header = “<head>头</head>”;
$smarty->assign(’header’,$header);
$smarty->display(header.tpl);
?>

header.tpl
{$header}

———————————————————————————–
嵌套结构二
index.php
    │ 
   
├─header.php
   
└─index.tpl
               └─header.tpl

这种结构,代码虽然可以运行,因为模块和模板不是配套调用,维护起来很费时费力,而且有时会破坏编码结构,代码如下:

index.php
<?php
/*模板设置*/
require_once($_SERVER["DOCUMENT_ROOT"].’/Smarty/Smarty.class.php’);
$smarty = new Smarty();

require_once(”./header.php”); 
//注意位置,文件开头引用会出现错误assign() on a non-object,语句要在配置smarty语句之后,当然你也把smarty设置保存到smarty.config.php,并且调用在requrie header.php之前

$smarty->template_dir = ‘./include/templates’;
$smarty->compile_dir = ‘./include/templates_c’;
$smarty->cache_dir = ‘./include/cache’;
$smarty->config_dir = ‘./include/config’;
/**********************************************************/

$index = “主页”;
$smarty->assign(’index’,$index);
$smarty->display(’index.tpl’);
?>
index.tpl
<html>
{include file=”header.tpl”}
<body>{$index}</body>
</html>

header.php
<?php
$header=”头”;
$smarty->assign(’header’,$header);
?>

header.tpl
<head>{$header}</head>

———————————————————————————–
嵌套结构三
index.php
    │ 
   
├─header.php
    │          └─header.tpl
   
└─index.tpl第三种结构,代码也可以运行,但是维护起来是最麻烦的,而且会严重破坏模板的编码结构,当你想要更改代码时,你不得不在两个页面中查询html标签是否匹配,代码如下:

 

index.php
<?php
/*模板设置*/
require_once($_SERVER["DOCUMENT_ROOT"].’/Smarty/Smarty.class.php’);
$smarty = new Smarty();

require_once(”./header.php”); 
//注意位置,文件开头引用会出现错误assign() on a non-object,语句要在new smarty()之后,当然你也把smarty设置保存到smarty.config.php,并且调用在requrie header.php之前

$smarty->template_dir = ‘./include/templates’;
$smarty->compile_dir = ‘./include/templates_c’;
$smarty->cache_dir = ‘./include/cache’;
$smarty->config_dir = ‘./include/config’;
/**********************************************************/

$index = “主页”;
$smarty->assign(’index’,$index);
$smarty->display(’index.tpl’);
?>
index.tpl
<body>{$index}</body>
</html>
header.php
<?php
$header=”头”;
$smarty->assign(’header’,$header);
$smarty->display(’header.tpl’);
?>

header.tpl
<html>

<head>{$header}</head>

———————————————————————————–
小结:通过上面的三种结构可以看出,如果使用模板,最好是将php文件与所匹配的模板想对应起来,既方便修改,代码的完整度也不会付出代价…
(由于smarty研究的不深,尚有不完善的地方希望谅解)

抱歉!评论已关闭.