Page 1 of 1

LISE template / or general question

Posted: Tue Mar 19, 2024 9:56 am
by smithdesign77
Hi!
I do have a working solution - but I wonder, if this could have been accomplished quicker/shorter in code.
I wanted to write a "last updated on" (German date, which is bad to sort by) at the very end of the LISE items.
This is what I have so far and what does work:

Code: Select all

<p><small>Last updated: {foreach from=$items item=item}
  {if !isset($highdate)}{assign highdate value=$item->modified_time|date_format:'%Y%m%d'}{/if}
  {if ($item->modified_time|date_format:'%Y%m%d' > $highdate)}{assign highdate value=$item->modified_time|date_format:'%Y%m%d'}{/if}
  {if $item@last}{$highdate|substr:-2}.{$highdate|substr:-4|truncate:2:''}.{$highdate|substr:-8|truncate:4:''}{/if}
{/foreach}</small></p>
If I only wrote {$highdate|date_format:'%d.%m.%Y'} the contents was wrong and the date was something in 1970. Hence all the substr and truncates.
I'd be glad to learn something - if you had the time to answer I'd appreciate it; again, this solution does the trick, so no biggie. ^-^

Re: LISE template / or general question

Posted: Tue Mar 19, 2024 12:14 pm
by velden
I'd expect the modified_time can already be compared without coverting it to a string.
Converting it to a string probably is the reason why you can't use {$highdate|date_format:'%d.%m.%Y'}
I wanted to write a "last updated on" (German date, which is bad to sort by) at the very end of the LISE items.
If you're already displaying (hence iterating) the items before, I'd include your logic inside that loop.
Once the loop is finished, you can just display the highdate.

The same applies to your loop (untested):

Code: Select all

{foreach from=$items item=item}
  {if !isset($highdate)}{assign highdate value=$item->modified_time|date_format:'%Y%m%d'}{/if}
  {if ($item->modified_time|date_format:'%Y%m%d' > $highdate)}{assign highdate value=$item->modified_time|date_format:'%Y%m%d'}{/if}
{/foreach}
<p><small>Last updated: {$highdate|substr:-2}.{$highdate|substr:-4|truncate:2:''}.{$highdate|substr:-8|truncate:4:''}</small></p>

Re: LISE template / or general question

Posted: Tue Mar 19, 2024 2:41 pm
by smithdesign77
Yup, you nailed it! ;D
My new code is now:

Code: Select all

{foreach original loop - no extra looping now…
{if !isset($highdate)}{assign highdate value=$item->modified_time}{/if}
{if ($item->modified_time > $highdate)}{assign highdate value=$item->modified_time}{/if}
{/foreach}
and then later only the variable {$highdate|date_format:'%d.%m.%Y'} — outputted with the correct format, without any substr, etc.
Awesome! THANKS!

Re: LISE template / or general question

Posted: Tue Mar 19, 2024 2:48 pm
by velden
Cool

Maybe you could even move the initial assign outside the loop (again untested):

Code: Select all

{$highdate=0}
{foreach original loop - no extra looping now…
{if ($item->modified_time > $highdate)}{assign highdate value=$item->modified_time}{/if}
{/foreach}