开发者

Using the Eco template engine with Rails

开发者 https://www.devze.com 2023-02-13 07:51 出处:网络
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?

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 b开发者_C百科oth the client and the server.


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


Put gem 'eco' in your Gemfile and name your files with .jst.eco and it should work just fine in Rails 3.1


[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.)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消