sstephenson / eco

Embedded CoffeeScript templates
MIT License
1.71k stars 70 forks source link

Helper functions that aren't tied to a model #25

Closed sandstrom closed 12 years ago

sandstrom commented 13 years ago

A nice addition would be helper functions that aren't tied to the model. One example would be a function to localize time (see below). If I've understood things correctly that would now require the localizedTime function on each model. If view/template helpers could be passed as as third argument that would be neat.

Or perhaps this is already possible, and I've just missed it.

var localizedTime = function(utcTime) {
    var offset = (new Date).getTimezoneOffset() / -60;
    // return a localized time (utcTime + offset)
} 
lagartoflojo commented 12 years ago

You could create and include a JavaScript object that has all these types of helpers. For example, you could have:

class Helpers
  localizedTime: (utcTime) ->
    (new Date).getTimezoneOffset() / -60

and then in your template:

<%= Helpers.localizedTime(@theTime) %>
sandstrom commented 12 years ago

Good idea!