深圳全飞鸿

标题: Smarty入门之hellowrold [打印本页]

作者: admin    时间: 2019-9-4 14:29
标题: Smarty入门之hellowrold
首先要知道,我们目前用的版本是3.1.33



-----------
1. 基本情况简介
-----------

首先打开Smarty.class.php文件看看里面的一些代码:

Smarty的构造器:

  1. templates:默认存放模板文件夹

  2. templates_c:默认存放混编文件的文件夹   (会自动创建)

  3. cache:存放缓存  (会自动创建)

  4. configs:存放配置文件
复制代码




-----------
2. 使用         
-----------  


先在templates文件夹下准备自己需要的模板文件。test1.html
  1.   <!DOCTYPE html>
  2.   <html>
  3.   <head>
  4.       <meta charset="utf-8">
  5.       <title>smarty test1</title>
  6.   </head>
  7.   <body>
  8.   它的名字叫{$name}
  9.   </body>
  10. </html>
复制代码




接着在根目录下建立访问的逻辑文件。test1.php
  1. <?php
  2. require './libs/Smarty.class.php';
  3. $smarty=new Smarty();
  4. $name='刘二狗';
  5. $smarty->assign( 'name' , $name );
  6. $smarty->display('./test1.html');
复制代码

作者: zhgc    时间: 2019-9-4 20:28
接着,马上验证{block}

在模板中放一下{syant}aa{/syant}

创建一个plugins目录,放以下内容 :

  1. <?php
  2.         function smarty_block_syant($params, $content, &$smarty, &$repeat) {
  3.                   // Smarty_Internal_Template
  4.                 trigger_error(get_class($smarty), E_USER_NOTICE);

  5.                 // MySmarty
  6.                 trigger_error(get_class($smarty->smarty), E_USER_NOTICE);
  7.         }
  8. ?>
复制代码


执行!
结果是找不到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