当前位置: 移动技术网 > IT编程>开发语言>PHP > Smarty环境配置与使用入门教程

Smarty环境配置与使用入门教程

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了smarty环境配置与使用方法。分享给大家供大家参考,具体如下:

下载smarty(这里以smarty-2.6.26为例)。解压下载的文件(目录结构还蛮复杂的)。接下来演示给大家一个安装实例,看过应该会举一反三的。

(1) 在根目录下建立了新的目录learn/,再在learn/里建立一个目录smarty/。将刚才解压缩出来的目录的libs/拷贝到smarty/里,再在smarty/里新建templates目录,templates里新建cache/,templates/,templates_c/, config/。

(2) 新建一个模板文件:index.tpl,将此文件放在learn/smarty/templates/templates目录下,代码如下:

<!doctype html public "-//w3c//dtdhtml 4.01
<html>
<head>
<metahttp-equiv="content-type" content="text/html;charset=gb2312">
<title>smarty</title></head>
<body>{#$hello#}</body>
</html>

新建index.php,将此文件放在learn/下:

<?php
require 'smarty/libs/smarty.class.php';
$smarty = new smarty;//设置各个目录的路径,这里是安装的重点
$smarty->template_dir ="smarty/templates/templates";
$smarty->compile_dir ="smarty/templates/templates_c";
$smarty->config_dir = "smarty/templates/config";
$smarty->cache_dir ="smarty/templates/cache";
//smarty模板有高速缓存的功能,如果这里是true的话即打开caching,但是会造成网页不立即更新的问题,当然也可以通过其他的办法解决
$smarty->caching = false;
$smarty->left_delimiter = "{#"; //重新定义边界,因为默认边界“{}“符,在html页面中嵌入js脚本文件编写代码段时使用的就是”{}“符,自定义边界符还可以是<{ }>, {/ /} 等
$smarty->right_delimiter = "#}";
$hello = "hello world!";//赋值
$smarty->assign("hello",$hello);//引用模板文件
$smarty->display('index.tpl');?>

(3) 执行index.php就能看到hello world!了。

更多关于php相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《php模板技术总结》、《php数组(array)操作技巧大全》、《php基于pdo操作数据库技巧总结》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于smarty模板的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网