编写 EJS 视图助手
我有一个生成 jst.ejs 模板的 Rails-backbone 项目。
我想在其中包含一些视图助手,但我花了很长时间才弄清楚如何将 EJS 或 JST 函数包含到该模板文件中。如果有人能够快速解释如何包含一个非常基本的函数,以便 ejs.jst 模板可以读取它,我将非常感激。
我尝试过侵入 JST & EJS,加上仅使用裸露的 javascript 函数,但没有带来任何乐趣。下面的示例尝试:
示例:(
# 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() %>
返回未捕获的引用错误)
任何想法表示赞赏。干杯。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要将其附加到
window
,因为coffeescript 在每个.coffee
文件周围放置了闭包()
。如何处理范围问题的一个很好的例子是任何流行的 js/coffee 实用程序,例如 underscore.js 。他使用var root = this
和exports
来符合 CommonJS 实践,并将他的_
函数引入到可以全局使用的世界中。CoffeeScript: Accelerated JavaScript Development 一书也有一章(第 4 章)讨论了这个主题因为它是一本关于 CoffeeScript 的好书。它解释了现代世界在 javascript 方面的很多情况。
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 usesvar root = this
andexports
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.