smarty 文件嵌套调用
来源:互联网 发布:linux 单用户启动 编辑:程序博客网 时间:2024/05/17 18:18
解决了困扰一天的字符验证的问题后,在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研究的不深,尚有不完善的地方希望谅解)
- smarty 文件嵌套调用
- smarty的嵌套循环
- smarty嵌套循环
- 001 - smarty 模板嵌套
- 头文件相互调用,相互嵌套解决方案
- smarty中section嵌套循环
- smarty调用php常量
- smarty从数据库调用
- Centos中用jna调用.so文件详细过程(.so文件嵌套调用.so文件)
- smarty的section嵌套循环一例
- smarty 模板内嵌套 css js问题
- smarty中section的嵌套用法
- smarty 模板 引入文件
- 【PHP】smarty初始化文件
- 【smarty】处理css文件
- smarty文件引用
- Smarty引用头文件
- smarty文件缓存
- 关于zxing项目j2me客户端编译
- Ajax对象XMLHttpRequest的常用属性,方法和事件
- 学习体验
- JQuery弹出层类库
- 本地连接SQL
- smarty 文件嵌套调用
- Ajax应用的五个步骤
- Linux 各种查看网卡流量的方法
- 嵌入式工程师是怎样炼成的(一)---- 引言
- 国内Nokia手机主要参数列表
- 手机站点流量统计系统分较
- 彻底解决J2ME中的中文问题
- QQ K809 MP5黑屏解决
- 中文编码规则一网打尽