Re: Adding stronger support for dates as built ins for "?today and ?yesterday

Daniel Dekany <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Tuesday, December 7, 2010, 10:45:12 PM, Ken Egervari wrote:

> Hi, I have a feature request.
>
> Today, I wrote a macro for formatting the date to be fairly pretty
> to look at. It separates the date and time into different styles,
> and if the date is today or yesterday, it says the actual word
> rather than the date. This is all very common type of stuff we do these days:
>
> <#macro formatDate date showTime=true>
>     <#if date??>
>         <span class="Date">
>             <#if date?date?string.short == today?date?string.short>
>                 Today
>             <#elseif date?date?string.short == yesterday?date?string.short>
>                 Yesterday
>             <#else>
>                 ${date?date}
>             </#if>
>         </span>
>         <#if showTime>
>             <span class="Time">${date?time}</span>
>         </#if>
>     </#if>
> </#macro>
>
> The only problem with this implementation is that "today" and
> "yesterday" need to be in the model. This is kind of a hassle to put
> these 2 objects in the model in every spot where they are used.
> Sure, they could be added in a base class controller and added to
> every page... but that's also kind of a waste too.
>
> It would be nice if freemarker added ?today and ?yesterday to
> return true/false when you use them with dates.

Getting the current date/time (or anything related to that) was such a
frequently asked thing during the years that .now and .today is on
my(?) TODO the next release. However to find out from there what was
today and what was on yesterday require some calendar math. While
adding ?is_today and ?is_yesterday is *maybe* OK, it should be only a
shorthand for something like .today = someOtherTimestamp?date and
.today?minus_days(1) == someOtherDate?date, but FM doesn't have
"minus_days" and such, and I think that's the more important issue
here.

Speaking of the next 2.3.x release... there are high-priority bug
fixes on the TODO and like. Seems I spend even less time with fixing
them than I expected, and it seems the others don't do much nowadays
either. So who knows when will it be out.

So, until the release, you don't have to put stuff like "today" into
the data-model if that makes tour app more messy instead of more
maintainable. Instead you can use something like
<#local today = "com.mycompany.myproject.freemarker.TodayModel"?new()>

> Another issue was that I needed to use ?date?string.short 4 times
> in the above code because ?date didn't properly return true when
> they were both equal. They returned false  

Are you sure? FM basically just does
left.getAsDate().compareTo(right.getAsDate()). So the result of the
comparison should solely depend on the non-FM APIs.

> I think people would use these built-ins if you provided them, and
> they would really clean up the model.
>
> Thanks for listening.
>
> Ken

-- 
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
What happens now with your Lotus Notes apps - do you make another costly 
upgrade, or settle for being marooned without product support? Time to move
off Lotus Notes and onto the cloud with Force.com, apps are easier to build,
use, and manage than apps on traditional platforms. Sign up for the Lotus 
Notes Migration Kit to learn more. http://p.sf.net/sfu/salesforce-d2d
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.