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

时间:2014-03-20 20:22:31

标签: php smarty

我希望我的用户能够使用不同的日期格式,而不使用Smarty附带的date_format选项,原因各不相同。

所以我试着跟随:

模板

{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);

但是每个条目的日期都是一样的。 有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您将继续覆盖并分配相同的全局变量。而是添加具有格式化时间的新数组键:

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

如果您愿意,也可以覆盖现有的time密钥。然后当你在模板中循环$row_entries时,只输出没有修饰符的格式化时间。

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

您可能有兴趣制作一个自定义修改器,将日期格式设置得更容易一些(如果您经常使用相同的格式),那么您可以使用以下内容:

{ $myDate | myDateFormat }