分享

Smarty3.1.18的配置和简单使用

 明天网吧 2015-08-20

1:首先下载Smarty包,地址有很多,这里就不说了

2:解压到apache的htdoc目录下,或者www目录,或者所使用的项目目录都可以

3:配置Smarty的一些基本参数,和Smarty同一目录下建一个include目录,在include里面建一个Smarty_inc.php的文件(这个可以变的,就是Smarty_inc.php名字可以任意取,路径也可以任意放,只要可以访问到,访问路径会正确就可以)

Smarty_inc.php内容如下:

<?php
//引入Smarty模板类
require_once("../Smarty/libs/Smarty.class.php");
//实例化Smarty类对象
$smarty=new Smarty;
//是否使用缓存,项目调试期间,不建议启用缓存
$smarty->caching=false;
//缓存生命周期
$smarty->cache_lifetime=120;
//设置配置目录
$smarty->setConfigDir("../configs");
//设置模板目录
//$smarty->template_dir="../templates";
$smarty->setTemplateDir("../templates");
//设置编译目录351721199305168049
//$smarty->compile_dir="../templates_c";
$smarty->setCompileDir("../templates_c");
//缓存文件夹
//$smarty->cache_dir="../smarty_cache";
$smarty->setCacheDir("../smarty_cache");
//左右边界符,默认是{},但实际应用中容易与JavaScript冲突,可以设计为<{ and }>这样的形式
//$smarty->left_delimiter="{";
//$smarty->right_delimiter="}";
?>
4:新建配置文件中的目录,configs, templates, templates_c, smarty_cache这些目录,都是和Smarty同一目录方便管理

我的目录结构是:

5:就是测试了,例如我的测试在图片中已经显示了就是test1.php

内容如下:

<?php

header("Content-Type:text/html; charset=gbk");
require_once "../include/Smarty_inc.php";
$titles=["PHP模板第一讲", "PHP模板第二讲", "PHP模板第三讲", "PHP模板第四讲"];
$contents=array("Smarty介绍", "Smarty应用的优缺点", "Smarty的配置", "Smarty的应用:变量,循环。。。。。");

//var_dump($title);
//var_dump($contents);
$head="PHP Templates Study";
$smarty->assign("headtitle", $head);
$smarty->assign("title", $titles);
$smarty->assign("content", $contents);
$smarty->display("test1.html");

?>

6:就是在templates目录下建立模板文件test1.html了

内容如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css1.css" />
<title>{$headtitle}</title>
</head>
<body>
<ol>
<!-- 模板中定义的循环访问数组的方式 -->
{section name=s loop=$title}
<li>{$title[s]}</li>
	<ul><li>{$content[s]}</li></ul>
{sectionelse}
none
{/section}
</ol>
</body>
</html>
这样就没有错误了

Hint:

引用目录一定要清楚目录结构,不然就是提示找不到文件或者目录了

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多