在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Smarty 是PHP的一个模板引擎,是由Monte Ohrt 和 Andrei Zmievski 使用PHP语言开发的,发展至今已成为一个非常流行的模板引擎,Smarty 提供了一种易于管理和使用的方法,将PHP代码从HMTL代码页面中分享出来,使用程序员与页面美工之间的分工更加明确,大大提高团队的开发效率。 1、要使用Smarty 可到官方网站http://www.smarty.net 去下载,smary2.x 和3.x区别: 【转】Smarty 3 API 的语法结构已经重构,使之更一致性和模块化,虽然为了向下兼容,仍然支持Smarty 2的语法,但会抛出一个被弃用的notice,虽然你可以屏蔽该notice,但强烈建议,在使用Smarty 3 时使用3的语法,一方面,Smarty 2的语法很可能在后面的版本中逐渐被取消,另一方面, Smarty2的语法,是对Smarty3的API的封装,所以性能方面也会有损失。 以下是 Smarty3与Smarty2的差别之处 1) 基本上,Smarty3的方法采用驼峰式的命名方式,如 fooBarBaz ; 2) 所有Smarty的属性都有get 和 set 的方法 如$smarty->cache_dir = ‘foo/’ 现在可以这样赋值 $smarty->setCacheDir('foo/'),同样可以通过 $smarty->getCacheDir() 来得到该属性值 ; 3) Smarty 3废除了一些如 ”is*”的方法,因为他们和现在的”get*”方法重复了 ; 4) Smarty 3 只能在PHP5下运行,不支持PHP4.; 5) {php} 标签默认是关闭的. 使用$smarty->allow_php_tag=true.开启 ; 6) 被空白包围的分隔符将不被解析,如{ foo }将不再作为smarty标签被解析,你必须使用{foo} 。 转自:http://www.5dcode.com/?p=339 下载完成按以下步骤安装: ; UNIX: "/path1:/path2" 改为: ; Windows: "\path1;\path2"
在PHP中实例化Smarty对象的方法: <?php // NOTE: Smarty has a capital 'S' require_once('Smarty.class.php'); $smarty = new Smarty(); ?>
现在,库文件已经放好了,可以开始为你的程序配置Smarty了:
在下面的安装例子中,我们将为一个留言本程序建立Smarty环境。 我们提供了一个目录命名约定的例子。 你可以为任何的程序建立同样的环境,仅需要修改 /usr/local/lib/Smarty-v.e.r/libs/ Smarty.class.php debug.tpl sysplugins/* plugins/* /web/www.example.com/ guestbook/ templates/ index.tpl templates_c/ configs/ cache/ htdocs/ index.php 明确你的web服务器文档根目录。在下面的例子中, 文档根目录是 将会有至少一个文件是放到文档根目录的,这个文件也会被浏览器访问到。 我们将这文件命名为 Smarty需要一些对目录的 读写权限 (windows用户请忽略),包括 我们需要创建文件 Example 2.8. /web/www.example.com/guestbook/templates/index.tpl {* Smarty *} Hello {$name}, welcome to Smarty! 现在,我们来修改 Example 2.9. 修改 /web/www.example.com/docs/guestbook/index.php <?php require_once(SMARTY_DIR . 'Smarty.class.php'); $smarty = new Smarty(); $smarty->setTemplateDir('/web/www.example.com/guestbook/templates/') $smarty->setCompileDir('/web/www.example.com/guestbook/templates_c/'); $smarty->setConfigDir('/web/www.example.com/guestbook/configs/'); $smarty->setCacheDir('/web/www.example.com/guestbook/cache/'); $smarty->assign('name','Ned'); //** un-comment the following line to show the debug console //$smarty->debugging = true; $smarty->display('index.tpl'); ?> (上面的setTemplateDir等不写都默认是这几个文件夹,所以不写也可以)说明在我们的例子中,我们为Smarty的目录使用了绝对路径。 如果 现在,用浏览器访问 你已经完成了Smarty的基础安装! 参考了:http://www.smarty.net/docs/zh_CN/installing.smarty.basic.tpl 稍微更灵活的方式是使用扩展类来安装Smarty和初始化。 代替反复地定义路径,赋同样的值等等,我们可以把这些操作放在一个地方进行。 我们新建一个目录 <?php // load Smarty library require('Smarty.class.php'); // The setup.php file is a good place to load // required application library files, and you // can do that right here. An example: // require('guestbook/guestbook.lib.php'); class Smarty_GuestBook extends Smarty { function __construct() { // Class Constructor. // These automatically get set with each new instance. parent::__construct(); $this->setTemplateDir('/web/www.example.com/guestbook/templates/'); $this->setCompileDir('/web/www.example.com/guestbook/templates_c/'); $this->setConfigDir('/web/www.example.com/guestbook/configs/'); $this->setCacheDir('/web/www.example.com/guestbook/cache/'); $this->caching = Smarty::CACHING_LIFETIME_CURRENT; $this->assign('app_name', 'Guest Book'); } } ?> 在 |
2022-08-15
2022-08-30
2022-08-17
2022-08-18
2022-07-18
请发表评论