malgorithms / toffee

a NodeJS and browser templating language based on coffeescript, with the slickest syntax ever
MIT License
174 stars 10 forks source link

Render page into string variable #7

Closed fjbsantiago closed 12 years ago

fjbsantiago commented 12 years ago

Hi Chris!

What if it were possible, on demand, before calling "res.render", to require and call Toffee, giving it the path to a partial, so Toffee could render it and return the result as a string.

This would make it possible, for example, to request html partials dinamically from the client, without having to hammer the html it in javascript.

Example of complete page request:

routes.coffee


toffee = require('toffee')

app.get   ' / ' , (req, res) ->
    res.locals.myPartial = toffee.renderPartial('partials/alertMessagePartial')

    res.render("partials/index")

layout.toffee


    //Somewhere in here
    { print myPartial }          //Would print alertMessagePartial.toffee

    { print body }                  //Would print index.toffee

Now, if an ajax request came in asking for alertMessagePartial


toffee = require('toffee')

app.get   ' /getPartial/alertMessagePartial ' , (req, res) ->
    myPartial = toffee.renderPartial('partials/alertMessagePartial ')

    res.send(myPartial)

How cool would this be? Then, on client side, we would just have to clear the main body container and replace it with the returned html !

Do you think this is doable?

malgorithms commented 12 years ago

You can call toffee to render a template whenever you want in Node. For example, you could pre-pub a partial and have the results of that call pubbed/cached in a variable if you wanted. If I understand what you're asking correctly, just use toffee.render, which expects 3 arguments:

  1. path to the template or partial
  2. the vars dictionary
  3. callback function, which expects err, res

For example:

toffee = require('toffee')
toffee.render '/partials/somePartial.toffee', {foo: "bar"}, (err, res) ->
   # do something with err, res

You could then take that res variable and pub that into your final page, if you wanted to. Or do whatever else you wanted with it.

Does that answer your question?

fjbsantiago commented 12 years ago

Yes it does Mr. Chris! Thank you. Lazy me, not making an effort and searching.

That's it

malgorithms commented 12 years ago

Great! I'm going to try to update the docs to make this clearer.