I have a rails-backbone project that generates jst.ejs templates.
I'd like to include some view helpers within there, but I'm having a helluva time figuring out how to include either EJS or JST functions into that template file. If anyone could offer a very quick explanation of how to include a very basic function so that it can be read by an ejs.jst template I'd be very appreciative.
I've tried hacking into JST & EJS, plus just using bare javascript functions, but nothing is bringing any joy. Example attempt below:
Example:
# helpers.js.coffee
console.log('yes, this file is being called from the app')
helloWorld: () ->
console.log "Hello, world!"
# app/assets/javascripts/backbone/templates/project/new.jst.ejs
<%= helloWorld() %>
(Returns uncaught referenceError)
Any ideas appreciated. Cheers.
You might need to attach that to window
, since coffeescript puts closures ()
around each .coffee
file. A good example of how to deal with scoping issues is any popular js/coffee utility, like underscore.js. He uses var root = this
and exports
to conform to CommonJS practices and get his _
function out into the world where it can be used globally.
The book CoffeeScript: Accelerated JavaScript Development has a chapter (chapter 4) on this very topic, as well as it's just a damn good book on CoffeeScript. It explains a lot of where the modern world is at javascript-wise.