免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3389 | 回复: 5
打印 上一主题 下一主题

Smarty怎么显示动态的内容呢? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-02 22:44 |只看该作者 |倒序浏览
我想使用smarty做一个显示时间的地方。主要是演试一下模版不变,数据变化。

希望的效果应该是每次刷新页面的时候,时间都会显示当前的时间。
但是现在发现有问题。无论怎么刷新都是只显示第一次访问的时间,我觉得也就是编译模版的那个时候的时间。
是不是我用错了什么?还是smarty就是只能显示静态的内容?
我觉得不应该不能变化的。不然模版没有变化,数据不断的在变化怎么处理呢?

这是我的php处理文件。
<?
$smarty = new Smarty();
$smarty->assign('time', date("H:i:s"));
echo date("H:i:s");
$smarty->display('index.tpl');
?>

这是模版文件(index.tpl)
<?
Now: {$time},  welcome to Smarty!<br/>
?>

论坛徽章:
0
2 [报告]
发表于 2008-10-03 06:05 |只看该作者
我写的代码
php文件
  1. <?php
  2.         include_once("../lib/Smarty.class.php");

  3.         $smarty = new Smarty();
  4.         $smarty->template_dir="./";
  5.         $smarty->compile_dir="../temp/";
  6.         $smarty->config_dir="../config/";
  7.         $smarty->cache_dir="../cache/";
  8.         $smarty->caching=false;
  9.        
  10.         //single
  11.         $smarty->lift_delimiter="{";
  12.         $smarty->right_delimiter="}";
  13.         //-------------------
  14.         //start of real code
  15.         //-------------------
  16.        
  17.         //checkBox 1
  18.         $smarty->assign('time', date("H:i:s"));
  19.         echo date("H:i:s");

  20.         $smarty->display("example04.html")
  21.         ?>
复制代码

html文件
  1. <html>
  2.         <head><title>time</title></head>
  3.         <body>
  4.         <p>Now: {$time}, hi</p>
  5.        
  6.         </body>
  7. </html>
复制代码


时间是变化的。

论坛徽章:
0
3 [报告]
发表于 2008-10-03 07:58 |只看该作者
自己查查资料呗。

论坛徽章:
0
4 [报告]
发表于 2008-10-03 08:08 |只看该作者
查看下cache文件夹
如果有文件,就要关闭cache

论坛徽章:
0
5 [报告]
发表于 2008-10-03 23:53 |只看该作者
Insert 函数类似 inluce 函数,不同之处是 insert 所包含的内容不会被缓存,每次调用该模板都会重新执行该函数.

{insert name="getBanner" lid=#banner_location_id# sid=#site_id#}

or

index.php:

require('Smarty.class.php');
$smarty = new Smarty;
$smarty->caching = true;

function smarty_block_dynamic($param, $content, &$smarty) {
return $content;
}
$smarty->register_block('dynamic', 'smarty_block_dynamic', false);

$smarty->display('index.tpl');


index.tpl:

Page created: {"0"|date_format:"%D %H:%M:%S"}

{dynamic}

Now is: {"0"|date_format:"%D %H:%M:%S"}

... do other stuff ...

{/dynamic}

论坛徽章:
0
6 [报告]
发表于 2008-10-04 14:06 |只看该作者
二楼的因为你设置了 $smarty->caching=false;所以smarty就不使用缓存了。
这样每次请求都要重新组装页面,效率就差了很远了。

fredyj方法不错。
我采用insert的方式从写了一下代码。现在贴出来和大家分享。
insert的资料比较少。有一个assign的属性一直不知道怎么用。估计搞明白了,连insert_函数都不用定义了。

php文件
<?php
require_once($_SERVER["DOCUMENT_ROOT"].'/class/Smarty_setup.class.php');

$smarty = new Smarty_Msgmanager();
$smarty->assign('time', date("H:i:s"));

function insert_getBanner() {
  return date("H:i:s");
}

$smarty->display('index.tpl');
?>

模版文件
<table border="1">
<tr>
<td>
动态的时间    {insert name="getBanner"}<br/>
被缓存了的时间{$time}<br/>
</td>
</tr>
</table>

[ 本帖最后由 moonight 于 2008-10-4 14:07 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP