深圳全飞鸿
标题:
Smarty入门之hellowrold
[打印本页]
作者:
admin
时间:
2019-9-4 14:29
标题:
Smarty入门之hellowrold
首先要知道,我们目前用的版本是3.1.33
-----------
1. 基本情况简介
-----------
首先打开Smarty.class.php文件看看里面的一些代码:
Smarty的构造器:
templates:默认存放模板文件夹
templates_c:默认存放混编文件的文件夹 (会自动创建)
cache:存放缓存 (会自动创建)
configs:存放配置文件
复制代码
-----------
2. 使用
-----------
先在templates文件夹下准备自己需要的模板文件。test1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>smarty test1</title>
</head>
<body>
它的名字叫{$name}
</body>
</html>
复制代码
接着在根目录下建立访问的逻辑文件。test1.php
<?php
require './libs/Smarty.class.php';
$smarty=new Smarty();
$name='刘二狗';
$smarty->assign( 'name' , $name );
$smarty->display('./test1.html');
复制代码
作者:
zhgc
时间:
2019-9-4 20:28
接着,马上验证{block}
在模板中放一下{syant}aa{/syant}
创建一个plugins目录,放以下内容 :
<?php
function smarty_block_syant($params, $content, &$smarty, &$repeat) {
// Smarty_Internal_Template
trigger_error(get_class($smarty), E_USER_NOTICE);
// MySmarty
trigger_error(get_class($smarty->smarty), E_USER_NOTICE);
}
?>
复制代码
执行!
结果是找不到syant这个block,为什么 ?
得出来的结论是,plugins的目录是要申明 的,加入以下一行就可以好
$smarty->setPluginsDir('plugins');
看官方的解释
https://www.smarty.net/docsv2/en/variable.plugins.dir.tpl
作者:
zhgc
时间:
2019-9-5 15:22
几个开关量要搞清楚 :
$smarty->debugging = true; //调试
$smarty->caching = true; //开启缓存,默认为false
$smarty->cache_lifetime = 120; //缓存存活时间(秒)
$smarty->force_compile = true; //强迫编译
$smarty->compile_check=false; //开发阶段默认true,发布时记得改为false
欢迎光临 深圳全飞鸿 (http://www.nagomes.com/disc/)
Powered by Discuz! X3.2