Skip to content
This repository has been archived by the owner on Sep 25, 2021. It is now read-only.

using custom functions within JavaScript Templates #4

Closed
tim-peterson opened this issue May 5, 2012 · 3 comments
Closed

using custom functions within JavaScript Templates #4

tim-peterson opened this issue May 5, 2012 · 3 comments

Comments

@tim-peterson
Copy link

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?

@blueimp
Copy link
Owner

blueimp commented May 7, 2012

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

@tim-peterson
Copy link
Author

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.

@blueimp
Copy link
Owner

blueimp commented May 7, 2012

You're welcome. :)

@blueimp blueimp closed this as completed May 14, 2012
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants