php使用smarty的简单实例demo

php使用smarty的简单实例demo
1、下载smarty将解压后的libs目录copy到项目目录下。
2、新建一个php文件,假如和libs目录同一级上。命名为smarty_test.php,然后增加两个目录一个为Templates文件夹,另一个为Templates_c目录,前者是以后模板文件要存放的目录,后者是smary编译后的文件存放目录。
3、在Templates目录下建立一个template.html文件,输入以下代码:
<body>
{$var1}
</body>

4、在smart_test.php中输入以下代码
<?php
define('MICE_DIR', str_replace('\\','/', dirname(__FILE__)).'/');
define('IN_MICECMS', TRUE);

include_once  MICE_DIR.'libs/Smarty.class.php';
//如果在php.ini文件中将include_path添加了smart的目录这里就直接写Smarty.class.php就可以了。

$smarty = new Smarty(); //建立smarty实例对象$smarty
$smarty->template_dir =  MICE_DIR."templates";//设置模板目录
$smarty->compile_dir =  MICE_DIR."templates_c"; //设置编译目录
//----------------------------------------------------
//左右边界符,默认为{},但实际应用当中容易与JavaScript
//相冲突,所以建议设成<{}>或其它。
//----------------------------------------------------
$smarty->left_delimiter = "{";
$smarty->right_delimiter = "}";

$smarty->assign("var1","this is a test by itlife365.com");
//编译并显示位于./templates下的a.html模板
$smarty->display('template.html');
?>


5、保存后,在浏览器中浏览smart_test.php会发现在template.html的模板文件中的{{var1}}被替换成了"this is a test by itlife365.com"。
6、查看templates_c 看到有smarty 编译成的php文件。

关于smarty 的更多详细用法请参考官网。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2025年4月    »
123456
78910111213
14151617181920
21222324252627
282930
搜索
标签列表
网站分类
最新留言
    文章归档
    友情链接

    Powered By Z-BlogPHP 1.7.3

    Copyright Your WebSite.Some Rights Reserved.闽ICP备11018667号-2