SmartTemplate 1.2.1 官方源文件 可以从http://download.youkuaiyun.com/source/670995下载
本系列文章中的实例代码可以到我的资源http://download.youkuaiyun.com/source/670977下载
以前学东西都是东一块西一块的,现在正好有时间,学点东西,同时也做一下记录,以便备忘,也希望可以帮到学习SmartTemplate 的同学,初学,文中难免有纰漏,如有错误或不同意见,希望可以跟我联系lxbin544930@163.com,我会及时更正,共勉。
1. Hello_Word
【Content】basic/Hello_Word.php:
- <?php
- require_once ('../config/smarttemplate_basic.cfg.php');
- $page = new SmartTemplate("Hello_World.html");
- $page->assign('TITLE', 'Hello World!');
- $page->output();
- ?>
【Template】templates/basic/Hello_Word.html:
- <html>
- <h3>{TITLE}</h3>
- </html>
【Return】
- <html>
- <h3>Hello World!</h3>
- </html>
【琐记】
这是 SmartTemplate 模板最最基础的应用实例,本实例仅仅演示了所有模板中都用到的最简单的赋值替换(Assign)方法,即用 assign('TITLE', 'Hello World!')将模板页中的 TITLE 替换为 Hello Word!
麻雀虽小,五脏俱全,该实例同时给出了 SmartTemplate 模板的基本应用流程:
- 配置PHP.INI文件中的include选项[ini_set('include_path',realpath('../include'));//此项配置对于basic和control_structures不是必需的,它的作用是在使用extensions时让系统找到include目录下的smarttemplate_extens使模板的mailto等扩展功能可以生效,所以在开发使用中最好写上。]
- 引入类文件[include_once("../include/class.smarttemplate.php");]
- 实例化模板类[new SmartTemplate("Hello_Word.html")]
- 模板页处理代码(入赋值等)[assign]
- 结果代码输出[output]
【附录】
config/smarttemplate_basic.cfg.php
- <?php
- ini_set('include_path',realpath('../include'));
- include_once("../include/class.smarttemplate.php");
- $_CONFIG = array(
- 'smarttemplate_compiled' => "../temp", /* temp 编译临时文件目录 */
- 'smarttemplate_cache' => "../temp", /* 缓存目录 */
- 'cache_lifetime' => 1, /* cache_lifetime 最小编译周期 */
- 'template_dir' => "../templates/basic" /* 模板目录 */
- );
- ?>
这里只是举了及其简单的单个模板变量赋值,下面的文章还是学习一下,复杂些的数组变量赋值吧,也就是basic目录下的Alternating_Colors.php,这是一个表格数据隔行换色的例子,其中会包括数据行数ROWCNT,数据行奇偶值ROWBIT,多行数据赋值[也可以叫数组赋值啦]。
明日任务:SmartTemplate 实例分析——基本语法[basic]之Alternating_Colors