Documentation on templating #7

Closed
wants to merge 2 commits into from

4 participants

@bradleyg

Some minimal documentation on how to use the handlebarsjs templating feature.

@adamghill

Maybe even more detailed documentation on how to use handlebars would be helpful?

@bradleyg
@jankolkmeier

I just started playing around with tako and found this working for me:

var index  = app.page().template('index.hbs')

app.route('/').html(function(req, res) {
    res.end(index.render({ foo:'bar' }))
})

That might not be the way to do it since I haven't truly grasped yet how templates/pages should be used in a "functional" web framework. I was actually trying to figure out if something along these lines would work (but tako's source was a bit difficult for me to understand):

app.route('/').page(index).json({ foo:'bar' });

I understand this in particular doesn't make much sense, but maybe you can see what I was looking for.
Whether or not Templates will stay in by default, it would be helpful if someone could write up some "best practices" of extending/using a functional web framework like tako?

EDIT: Added second example.

@adamghill

Thanks, @jouz. I'll have to try that out soon.

@adamghill

@jouz The index variable was always undefined when I did your example (maybe you are on an older version?). However, this worked for me:

var tako = require('tako')
  , path = require('path')
  ;

var app = tako()
  , templates = app.templates;

templates.directory(path.join(__dirname, 'views'))

app.route('/').html(function(req, res) {
  templates.get('index.html', function(err, t) {
    if (err) throw new Error(err)
    res.end(t.render({ foo: 'bar' }))
  })
})
@jankolkmeier

Oh yes, sorry - I forgot to paste the line where the template directory is set!

@saambarati

@adamghill , this is how I am using templates as well. I believe the templates object will cache all of Handlebars compiled functions against the name of your template, both with and without the file extension.

After reading the source on the Page objects, I am still not quite sure how to use them. Like you, I have just been using the app.templates.get method for fetching templates and rendering them.

If @mikeal keeps templates in, I think documenting it will be helpful, and I can help write some more detailed docs if it is needed.

@bradleyg bradleyg closed this Jun 29, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment