blueimp/JavaScript-Templates

using custom functions within JavaScript Templates

Closed this issue · 3 comments

In my template I'd like to format my dates and have them update periodically.

I saw the following in the documentation:

<span>Year: {% var d=new Date(); print(d.getFullYear()); %}</span>

my question is how to use my own custom function, nicetime(), in my template instead of getFullYear().

here's what i'd like to work, but doesn't:

<span>Year: {% var d=nicetime(); print(d); %}</span>

here's what nicetime() looks like:

function nicetime(){ var time = new Date(), comment_date = setInterval(function() { var time2 = time_since(time.getTime()/1000); return time2; // 1 second ago, then 2 seconds ago, and so on... }, 1000); }

time_since() formats the code in Facebook-style: "2 seconds ago...".

any thoughts would be greatly appreciated?

The JavaScript Templates generate a string, not a DOM collection, so you can't use setInterval in the way you intended.
I would advise you to make use of a span element with a specific ID that can be updated periodically by updating the DOM element with the specified ID.
That ID could be generated by a custom helper function.

On how to use custom helper functions, please have a look at the section "Local helper variables" at the documentation:
https://github.com/blueimp/JavaScript-Templates/blob/master/README.md

hi Sebastian, thanks for the reply. Sorry to have bothered you again on this. It took me a long time, but I actually just arrived at exactly the solution you just said, try this jsfiddle: http://jsfiddle.net/SpYXM/89/. It works!

Again, thanks for your help. I'm using your Multi-upload plugin too (like the rest of the world). You do really great stuff. Thanks for taking the time for us little people.

You're welcome. :)