Question

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?

Was it helpful?

Solution

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 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top