sstephenson/eco

Support Node.js Express?

aseemk opened this issue · 9 comments

Hey there,

Great work on this stuff! Just one request: is it possible to use this as a template engine in Express?

I'm not sure exactly what that entails, but I don't think it's much; I'm pretty sure it's just a matter of exposing the expected APIs. I can ask the Express author(s) for details on this if you would like.

Here's the sample app I made to test this: https://gist.github.com/927144 (Check out the comments to see the error message.)

Thanks in advance!

Cheers,
Aseem

I use eco in express (v 2.3.3) registering .html like this

app.set 'view engine', 'html'
app.register '.html', 
   compile: (str, options) ->
      return (locals) ->
         eco.render str, locals

Thanks @yizzreel -- that works like a charm! I'll keep this issue open; it would still be great for Eco to support Express out-of-the-box.

If you take a quick look at https://github.com/sstephenson/eco/blob/master/src/eco/compiler.coffee#L6, you'll see that the problem registering eco as a processor in expresso is the call to the method compile, which in eco returns a string to be interpreted by the class Function() (that takes the list of arguments and the function body string in the constructor), while express expects it to return a callback, so it tries to use that returned string as a the expected callback, hence the error you're getting in your gist page.

I think express recently changed the default method for view rendering from render to compile, so this error might be new, since express and eco are being used together in https://github.com/sstephenson/stitch (not sure which versions though, I'm still kinda new in all this coffeescript awesomeness).

Thanks for looking into it! I'm not knowledgeable enough to understand then what the right course of action is w.r.t. backwards compatibility, etc., but one quick comment:

since express and eco are being used together in https://github.com/sstephenson/stitch

Hmm, are you sure? I see the sample code mentions Stitch is to be used with Express, but I don't see any reference to Eco anywhere. The source also isn't an Express app.

See https://github.com/sstephenson/stitch/blob/master/src/stitch.coffee#L20. And, it's true, express is not part of stitch, but they work together nicely.

Apologies if I'm still being dense, but it still doesn't seem to use Express's view system for compiling Eco -- which is what my issue is about. Instead, Stitch seems to compile Eco directly.

Cool addition in your fork. =) Plan on submitting it as a pull request? Btw, maybe it would be better named express to make it clear this is the property to use with Express?

The pull is already submitted :) #19 (it's also in this comment thread, above your last comment). I used the less intrusive name I could think of, to make the smallest change possible.

Fixed in 5d62cdd.

express = require "express"
eco = require "eco"

app = express.createServer()
app.register ".eco", eco

Awesome! Could you bump the version number and npm publish this? Thanks!