PHP - Smarty模版使用(一) - 配置及简单调试

1.需要libs(文件夹,附件稍后打包上传),内含smarty类

2.目录结构自行创建,附自使用结构:

config/

-> site.conf  // 名称后缀请自定义

libs/

-> ....//见附件

plugins/

-> ...//后续讲解

runtime/

-> cache/

-> compile/

template/

-> 1.html

smarty.php

注:"->" 后面的表示存在于上一行文件夹之下,因为tab留空再发表之后就会全左对齐了,所以暂时这样显示吧

3. site.conf配置文件

sitename=河南开拓机械
siteurl=http://www.hnktjx.com

配置文件采用键名对的形式,左边键名,右边键值,右边值内容无需引号包围

4.smarty.php代码

<?php
/* Smarty使用 */

	//1.加载smarty核心类
	include "./libs/Smarty.class.php";

	//2.实例化对象
	$smarty = new Smarty;

	//3.初始化配置
	$smarty->setTemplateDir("./template");//设置模版路径
	$smarty->setCompileDir("./runtime/compile");//设置编译路径
	$smarty->setCacheDir("./runtime/cache");//设置缓存路径
	$smarty->setConfigDir("./config");//设置配置文件路径
	$smarty->addPluginsDir("./plugins");//添加插件路径
	$smarty->caching = 0;//初始化缓存
	$smarty->cache_lifetime = 3600;//缓存超时时间
	$smarty->left_delimiter = "<{";//左定界符
	$smarty->right_delimiter = "}>";//右定界符

	//4.发送变量
	$smarty->assign("title","Smarty测试");
	$smarty->assign("content","这里是我的页面内容<hr/>");

	//5.显示模板
	$smarty->display("1.html");
?>
5. 1.html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title><{$title}></title>
</head>
<body>
	<{$content}>
	开始加载配置文件。。。<hr/>
	<{config_load file="site.conf"}>
	网站名称:<{#sitename#}><br/>
	网站地址:<{#siteurl#}><br/>
</body>
</html>

模版内容中使用定界符,内包围发送来的变量名,即可获得数值

另加载配置文件还有另一种方法:

网站名称:<{$smarty.config.sitename}>
网站地址:<{$smarty.config.siteurl}>



以上即为Smarty的初步认知及使用,如果错误欢迎指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值