Home  >  Article  >  Backend Development  >  php smarty框架的初步使用和注意事项

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

WBOY
WBOYOriginal
2016-06-20 13:04:471089browse

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="";
        
    2.3Smarty使用注意事项
        1.查找资源(css/js/image)
            资源文件放在访问的php页面对应的资源位置(因为访问时模板是被包含在php文件当中的)
        2.所有的display模板时(还是include),都要指定Smarty模板中的指定的基路径

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

示例代码:

init.smarty.php

 

template_dir=ROOT."./templates/";
	$tpl->compile_dir=ROOT."./template_c/";
	$tpl->left_delimiter="";
	?>


mysmarty.php

 

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


mysmarty.html




<!--{$title}-->





Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn