interlock / connect-handlebars

Connect middleware for Handlebars

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

returns self, not result

lucasgonze opened this issue · comments

Using Connect 2.2.2.

Here is my source:

var connect = require('connect'),
    http = require('http'),
    connect_handlebars = require('connect-handlebars');

var app = connect()
.use(connect.favicon())
.use(connect.logger('dev'))
.use("/templates.js", connect_handlebars("."))
.use(connect.static('public'))
.use(connect.directory('public'))
.use(connect.cookieParser('my secret here'))
.use(connect.session())
.use(function(req, res) {
    res.end('Hello from Connect!\n');
});

http.createServer(app).listen(3000);

On execution that displays the following:

(function() {;
  var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['my'] = template(function (Handlebars,depth0,helpers,partials,data) {
  helpers = helpers || Handlebars.helpers;
  var buffer = "", stack1, foundHelper, self=this, functionType="function", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;


  buffer += "<blink>hello</blink>\n\n<p>";
  foundHelper = helpers.value;
  stack1 = foundHelper || depth0.value;
  if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
  else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "value", { hash: {} }); }
  buffer += escapeExpression(stack1) + "</p>\n\n";
  return buffer;});
templates['node_modules/connect-handlebars/specs/templates/basic'] = template(function (Handlebars,depth0,helpers,partials,data) {
  helpers = helpers || Handlebars.helpers;
  var buffer = "", stack1, foundHelper, self=this, functionType="function", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;


  buffer += "<p>";
  foundHelper = helpers.value;
  stack1 = foundHelper || depth0.value;
  if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
  else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "value", { hash: {} }); }
  buffer += escapeExpression(stack1) + "</p>";
  return buffer;});
templates['node_modules/connect-handlebars/specs/templates/onedeep/child'] = template(function (Handlebars,depth0,helpers,partials,data) {
  helpers = helpers || Handlebars.helpers;
  var buffer = "", stack1, foundHelper, self=this, functionType="function", helperMissing=helpers.helperMissing, undef=void 0, escapeExpression=this.escapeExpression;


  buffer += "<span class=\"child\">";
  foundHelper = helpers.date;
  stack1 = foundHelper || depth0.date;
  if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
  else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "date", { hash: {} }); }
  buffer += escapeExpression(stack1) + "</span>";
  return buffer;});
})();

That is, your code isn't executed, it's just dumped out.

I believe this is caused by the "." path being used. In many cases you will want to use something like this:`

.use("/templates.js", connect_handlebars(__dirname + "/path))

I'll update the docs to mention that. I believe modules have self referencing paths for ".", not much I can do about that.

And done.

My reproduction case actually put the template file in the current directory, so the "." path was correct.

$ find .| grep -v node_modules
.
./app.js
./my.handlebars

Here's app.js with even more stripped out:

var connect = require('connect'),
    http = require('http'),
    connect_handlebars = require('connect-handlebars');

var app = connect().use("/templates", connect_handlebars(__dirname)+"/templates");

http.createServer(app).listen(3000);

Here's my.handlebars:

<blink>hello</blink>

I had to modify your example to work:

var app = connect().use("/templates.js", connect_handlebars( __dirname ) ); 

I can see how the recursive searching is going to find templates in node_modules. You haven't described your use case very much, but generally I see most projects not leaving their templates in the root directory (often in ./views or ./templates). If all your templates are in your root directory, you could disable the recursive search option.

var app = connect().use("/templates.js", connect_handlebars(__dirname, { recursive: false } ) );

I think this covers most common cases. I think explicitly exclude node_modules is fairly edge case for the common uses, but it may make sense to add an exclude option to filter stuff out anyways. I can't think of any particularly useful user stories for that, since nearly off of them would be solved by putting templates in their own folder.

If you submit a pull request with that feature and some specs to go with it thou, I'll merge it in.