• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php smarty框架的初步使用和注意事项

php 搞代码 4年前 (2022-01-23) 10次浏览 已收录 0个评论

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=”<!—ecms {";
$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="<!---ecms {";	$tpl->right_delimiter="}-->";	?>

mysmarty.php

<?php	//如果文件加载失败require会停止继续解析php;而include则会<span style="color:transparent">/本文来源gaodai#ma#com搞*!代#%^码网%</span><sub>搞代gaodaima码</sub>继续向下执行	require 'init.smarty.php';	//程序简单方式	$title="这是一个文字标题,从数据库中获取";	$content = "这是内容";		$tpl->assign("title",$title);	$tpl->assign("content",$content);	//模板文件名可以随便定义:比如:mysmarty.tpl只有内容是html就可以了	$tpl->display("mysmarty.html");?>

mysmarty.html

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title><!---ecms {$title}--></title><body><!---ecms {$content}-->

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php smarty框架的初步使用和注意事项

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址