On Thu, Mar 17, 2011 at 3:11 PM, James.Q.L <[email protected]> wrote:
>
> nice to see you again, Cees - We went to the 2006 chicago yapc from to.pm. 
> but I am now in Beijing.

I knew I recognized the name but couldn't put a place or a face to it.
 It's all coming back to me now.  Makes me want to head to another
YAPC, but I'm in Sydney Australia these days...

> I have never touched macro before and I ought to learn more about TT.
>
> By the way. I got a suggestion from Stackoverflow to use custom plugin or 
> vmethod and I like that way better.

There is an alternate solution as well.  Assuming that this is for an
HTML page, and you are not against using JavaScript, I have found the
jquery timeago plugin handy for this type of thing.
(http://timeago.yarp.com/)

<abbr class="timeago" title="[% date %]">[% date.strftime('%B %e, %Y
%r') %]</abbr>
<script>
  $("abbr.timeago").timeago();
</script>

The beauty of this method is that the date is updated in real time
while the page is left open.  So if an event happened 'a few minutes
ago' when the page was opened and the page was left open for an hour,
the jquery plugin keeps updating the date to keep the info current
(see the example page to see how it works).

I also like that it greatly simplifies the logic in my templates.
Once again, I use a macro to render this for me (note that in the
example I only use the timeago formatting when the date is less than 2
days old, otherwise I just show the real date).  Also, the
loader.jQuery thing is something I built in house to allow us to load
and sandbox multiple versions of jQuery and any required plugins
simply and efficiently.  All you need to do is replace that bit and
ensure that jquery and jquery.timeago are loaded for any pages that
use the plugin.

[% inline_script = BLOCK %]
    loader.jQuery({
        version  : '1.4.2',
        callback : function ($) {
            $.plugin('timeago',function(){
                $("abbr.timeago").timeago();
            });
        }
    });
[% END;
   javascript.push( content=inline_script );

   MACRO format_date(date, options) BLOCK;
     IF NOT options.size;
       options = {};
     END;
     format = options.format || '%B %e, %Y';
     longformat = options.longformat || '%B %e, %Y %r';
     USE now = DateTime(now=1, time_zone='Australia/Sydney');
     IF now < date && now.clone.add( days => 2) > date;
       # This date is less than 2 days in the future
       SET fuzzy = 1;
     ELSIF now > date && now.clone.subtract( days => 2) < date;
       # This date is less than 2 days in the past
       SET fuzzy = 1;
     END;
     IF fuzzy; %]
<abbr class="timeago" title="[% date %]">[% date.strftime(longformat) %]</abbr>
[%   ELSE %]
<abbr title="[% date.strftime(longformat) %]">[% date.strftime(format) %]</abbr>
[%   END;
   END;
%]


The above is a bit ugly, but it is written once and doesn't need to be
looked at again.  From the templates everything looks much cleaner as
all I need is this:

[% PROCESS format.macro # <== load the macro and inject required javascript %]
finished : [% format_date(finish) %]

Cheers,

Cees

_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to