Having problems with a complex {assign} statement

From: Date: Tue, 23 Jul 2002 23:49:57 +0000
Subject: Having problems with a complex {assign} statement
Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-5401@lists.php.net to get a copy of this message
I'm having problems with an {assign} statement in a Smarty template and I'm not sure if it's a problem with my code or a Smarty bug. I'm currently rewriting my blog to use Smarty for templating, and I'm stuck on the code to keep all of the day's entries under a single date heading (as seen here: http://www.bath.ac.uk/~cs1spw/blog/ ) I'm feeding Smarty an array of BlogEntry objects called $entries - my template looks like this: ==== {assign var="currentDate" value=""} {foreach key=key item=entry from=$entries} {include file="db:entry"} {/foreach} ==== The "entry" template looks like this: ==== {assign var="theDate" value=$entry->added|date_format:"%D %F %Y"} {if $currentDate != $theDate} <em>Debugging info: currentDate is {$currentDate} and theDate is {$theDate}</em><br /> {assign var="currentDate" value=$theDate} <h3 class="dateHeader">{$currentDate}</h3> {/if} {* Code for displaying the $entry goes here *} ==== The logic is pretty simple. The "current" date is assigned to the $currentDate variable and this is checked against the $theDate variable (created by running the entry's "added" timestamp through a date format) to see if it has changed. If it /has/ changed, display the date header - otherwise just display the entry. This logic works fine on my current blog where it is implemented in pure PHP, but in my Smarty implementation the date header is displayed every time round. My debugging info shows that currentDate is /never/ assigned a value. In other words, the following line is having no effect: {assign var="currentDate" value=$theDate} However, $theDate /is/ having a value assigned to it. Incidentally, before you suggest moving the above logic to PHP I've thought about it and I'd rather not - having a single date displayed above the day's entries is a presentation logic thing and as such I would rather keep it in the Smarty templates. Thanks, Simon Willison

« previous php.smarty.general (#5401) next »