douzhushen_9776 2014-03-20 20:22
浏览 42
已采纳

使用Smarty在博客条目中定义日期

I want my users to be able to use different date formatting without using the date_format option Smarty comes with, for different reasons.

So i tried following:

Template

{foreach from=$list_entries item=row_entries}
 <h1>{$row_entries.title}</h1>
 <p>{$row_entries.content}</p>
{/foreach}

PHP

$blog_template = mysqli_fetch_assoc(mysqli_query($con,"SELECT * FROM installed_designs WHERE u_id='$current_user_id' AND standard='1'"));

$smarty = new Smarty();

$result_entries = $con->query("SELECT * FROM entries");
$list_entries=array();
while ($row_entries = $result_entries->fetch_assoc())
{
$list_entries[]=$row_entries;
}


$smarty->assign('TemplateCSS', $blog_template["css"]);

$template_string = $blog_template["template"];
$smarty->assign('list_entries', $list_entries);

// Debugging and caching
$smarty->debugging = false;
$smarty->caching = false;
$smarty->cache_lifetime = 0;
$smarty->display('string:'.$template_string);

But the date remains same in every entries. Is there a way to work this out?

  • 写回答

1条回答 默认 最新

  • duanpiyao2734 2014-03-20 20:26
    关注

    You keep overwriting and assigning the same global variable. Instead, add a new array key with the formatted time:

    $row_entries["formatted_time"] = date("H:s:i", $row_entries["time"]);
    $list_entries[] = $row_entries;
    

    You can also just overwrite the existing time key if you wish. Then when you loop though $row_entries in your template, just output the formatted time without a modifier.

    { foreach from=$list_entries item=row }
       Time: { $row->formatted_time }
    { /foreach }
    

    You might be interested in making a custom modifier that formats dates a little easier (if you use the same formatting a lot), so you can use something like:

    { $myDate | myDateFormat }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?