将 Eco 模板引擎与 Rails 结合使用
Eco 是一种嵌入式 Coffeescript 模板语言。它类似于 ERB。有没有办法可以在 Rails 应用程序中将其用作服务器端模板语言?
最终目标是在客户端和服务器上使用一些用 Eco 编写的部分。
Eco is an embedded Coffeescript templating language. It resembles ERB. Is there a way I can use this as a server-side templating language in a Rails app?
The ultimate goal is to use some partials written in Eco on both the client and the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我刚刚注意到 Rails 3.1 将支持这一点。基本上 Sam Stephenson 刚刚发布了 ruby-eco 并连接了链轮来使用它。因此,这意味着在视图模板中使用 .eco 现在就可以工作了。
链接到提交
I just noticed that Rails 3.1 will support this. Basically Sam Stephenson just published ruby-eco and hooked up sprockets to use it. So this means using .eco in your view templates will now just work.
Link to the commit
https://github.com/sstephenson/ruby-eco
https://github.com/sstephenson/ruby-eco
将
gem 'eco'
放入 Gemfile 中,并使用.jst.eco
命名文件,它应该在 Rails 3.1 中正常工作Put
gem 'eco'
in your Gemfile and name your files with.jst.eco
and it should work just fine in Rails 3.1[更新:正如 Ben 在下面指出的那样,sstephenson(Eco 的创建者和 Rails 核心团队的成员)创建了名为 ruby-eco 让两者可以很好地协同工作。我最初的答案(如下)早于该项目。]
不可能直接执行此操作,因为 Eco 运行在 Node.js 上,并且您无法直接在 Rails 中运行 Node.js(请参阅 此相关问题)。
您可以代理从 Rails 应用程序到在不同端口上运行的 Node.js 应用程序的某些路由,但是没有任何简单的方法可以在两个应用程序之间传递信息,因此这可能毫无意义。
目前,Eco 与 Rails 是一个非此即彼的决定。但是,如果您不愿意从 Rails 跨越到 Node,您仍然可以使用 Johnson,它允许您从 Ruby 运行 JS。 (据我所知,还没有人编写过 CoffeeScript-Johnson 集成 - 不过,您可以使用
coffee -pe
命令将 CoffeeScript 转换为 JavaScript,自己轻松编写一个集成。)[Update: As Ben points out below, sstephenson—the creator of Eco and a member of the core Rails team—has created something called ruby-eco that lets the two play nice together. My original answer, below, predates that project.]
It's not possible to do it directly, because Eco runs on Node.js, and you can't run Node.js directly within Rails (see this related question).
You could proxy certain routes from your Rails app to a Node.js app running on a different port, but there wouldn't be any easy way of passing information between the two applications, so this would likely be pointless.
For the time being, Eco vs. Rails is an either-or decision. But if you're not willing to make the leap from Rails to Node, you can still do JavaScript templating on the server side using Johnson, which lets you run JS from Ruby. (To my knowledge, no one has written a CoffeeScript-Johnson integration yet—you could write one yourself pretty easily, though, using the
coffee -pe
command to convert CoffeeScript to JavaScript.)我有一个项目,使用 Eco inside Rails 作为安全模板系统,如 Liquid。我称之为 Ice,位于 http://github.com/ludicast/ice。
最初我使用了一些通用的 javascript 模板引擎,但围绕 eco 聚集的蒸汽使其成为一个简单的选择。
I have a project using eco inside rails as a secure templating system like Liquid. I call it Ice and is over at http://github.com/ludicast/ice.
Initially I used some generic javascript templating engine but the steam gathering around eco made it an easy choice.