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 }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本