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

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>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值