SmartTemplate 实例分析——基本语法[basic]之Hello_Word

本文介绍如何使用SmartTemplate进行基础的模板变量赋值操作,并通过Hello World示例展示配置及使用流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

  1. <?php
  2.     require_once ('../config/smarttemplate_basic.cfg.php');
  3.     $page = new SmartTemplate("Hello_World.html");
  4.     $page->assign('TITLE''Hello World!');
  5.     $page->output();
  6. ?>

【Template】templates/basic/Hello_Word.html:

  1. <html>
  2. <h3>{TITLE}</h3>
  3. </html>

【Return】

  1. <html>
  2. <h3>Hello World!</h3>
  3. </html>

【琐记】
    这是 SmartTemplate 模板最最基础的应用实例,本实例仅仅演示了所有模板中都用到的最简单的赋值替换(Assign)方法,即用 assign('TITLE''Hello World!')将模板页中的 TITLE 替换为 Hello Word!

麻雀虽小,五脏俱全,该实例同时给出了 SmartTemplate 模板的基本应用流程:

  1. 配置PHP.INI文件中的include选项[ini_set('include_path',realpath('../include'));//此项配置对于basic和control_structures不是必需的,它的作用是在使用extensions时让系统找到include目录下的smarttemplate_extens使模板的mailto等扩展功能可以生效,所以在开发使用中最好写上。]
  2. 引入类文件[include_once("../include/class.smarttemplate.php");]
  3. 实例化模板类[new SmartTemplate("Hello_Word.html")]
  4. 模板页处理代码(入赋值等)[assign]
  5. 结果代码输出[output]

【附录】
config/smarttemplate_basic.cfg.php

  1. <?php
  2.     ini_set('include_path',realpath('../include'));
  3.     include_once("../include/class.smarttemplate.php");
  4.     $_CONFIG = array(
  5.         'smarttemplate_compiled' => "../temp",  /* temp 编译临时文件目录 */
  6.         'smarttemplate_cache' => "../temp",     /* 缓存目录 */
  7.         'cache_lifetime' => 1,                  /* cache_lifetime 最小编译周期 */
  8.         'template_dir' => "../templates/basic"  /* 模板目录 */
  9.     );
  10. ?>

这里只是举了及其简单的单个模板变量赋值,下面的文章还是学习一下,复杂些的数组变量赋值吧,也就是basic目录下的Alternating_Colors.php,这是一个表格数据隔行换色的例子,其中会包括数据行数ROWCNT,数据行奇偶值ROWBIT,多行数据赋值[也可以叫数组赋值啦]。

明日任务:SmartTemplate 实例分析——基本语法[basic]之Alternating_Colors

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值