您的位置:首页 > 编程语言 > PHP开发

php学习笔记(三十四)smarty框架的初步使用和注意事项

2013-01-07 14:13 791 查看
1.什么是模板引擎

两个部分比较关心

1.1就是GUI(视图,界面) --HTML CSS JAVASCRIPTE

1.2是业务逻辑(php编码) --PHP MYSQL

模板引擎是使用php开发的一个项目

2.Smarty

2.1安装

1.解压,只用到libs目录,需要使用Smarty.class.php和插件文件夹

2.创建文件夹:templates和template_c文件夹(默认文件夹名)

3.创建php文件(见例子)

include "./libs/Smarty.class.php";

设置替换内容,设置显示模板

4.访问

2.2Smarty初始化

1.初始化可以在Smarty.class.php中修改制定值

2.在php页面调用的时候初始化

//解决问题:Warning: strftime() [function.strftime]:

date_default_timezone_set("Asia/Shanghai");

include "./libs/Smarty.class.php";

$tpl = new Smarty();

//smarty初始化

$tpl->template_dir="./templates/";

$tpl->compile_dir="./template_c/";

$tpl->left_delimiter="<!--{";

$tpl->right_delimiter="}-->";

2.3Smarty使用注意事项

1.查找资源(css/js/image)

资源文件放在访问的php页面对应的资源位置(因为访问时模板是被包含在php文件当中的)

2.所有的display模板时(还是include),都要指定Smarty模板中的指定的基路径

3.模板目录之外的php引用smarty文件时,将Smarty的初始文件换成绝对路径

示例代码:

init.smarty.php

<?php

define(ROOT, "");

//解决问题:Warning: strftime() [function.strftime]:
date_default_timezone_set("Asia/Shanghai");
include ROOT."./libs/Smarty.class.php";

$tpl = new Smarty();

//smarty初始化
$tpl->template_dir=ROOT."./templates/";
$tpl->compile_dir=ROOT."./template_c/";
$tpl->left_delimiter="<!--{";
$tpl->right_delimiter="}-->";
?>


mysmarty.php

<?php
//如果文件加载失败require会停止继续解析php;而include则会继续向下执行
require 'init.smarty.php';
//程序简单方式
$title="这是一个文字标题,从数据库中获取";
$content = "这是内容";

$tpl->assign("title",$title);
$tpl->assign("content",$content);
//模板文件名可以随便定义:比如:mysmarty.tpl只有内容是html就可以了
$tpl->display("mysmarty.html");
?>


mysmarty.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><!--{$title}--></title>
</head>
<body>
<!--{$content}-->
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐