Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates break when empty #18

Closed
Fauntleroy opened this issue Apr 12, 2013 · 9 comments
Closed

Templates break when empty #18

Fauntleroy opened this issue Apr 12, 2013 · 9 comments

Comments

@Fauntleroy
Copy link

If I run an empty template through express3-handlebars, I get this response from every template:

Error: You must pass a string or Handlebars AST to Handlebars.compile. You passed 
    at new Error (unknown source)
    at Error.Handlebars.Exception (/Users/Tim/Work/repos/solidus/node_modules/express3-handlebars/node_modules/handlebars/lib/handlebars/utils.js:8:41)
    at exports.attach.Handlebars.compile (/Users/Tim/Work/repos/solidus/node_modules/express3-handlebars/node_modules/handlebars/lib/handlebars/compiler/compiler.js:1254:11)
    at extend.loadTemplate (/Users/Tim/Work/repos/solidus/node_modules/express3-handlebars/lib/express-handlebars.js:137:46)
    at extend._loadFile (/Users/Tim/Work/repos/solidus/node_modules/express3-handlebars/lib/express-handlebars.js:296:35)
    at fs.readFile (fs.js:176:14)
    at fs.close (/Users/Tim/Work/repos/solidus/node_modules/grunt/node_modules/rimraf/node_modules/graceful-fs/graceful-fs.js:90:5)
    at Object.oncomplete (fs.js:297:15)
@ericf
Copy link
Owner

ericf commented Apr 12, 2013

Can you describe the steps so that I can reproduce this? Is it just an empty template file, or a template file which does not exist on the file system?

@Fauntleroy
Copy link
Author

It's any empty template file. Here are exact steps to reproduce:

  1. server.js
var express = require('express');
var router = express();
var express_handlebars = require('express3-handlebars');
var express_handlebars_config = {
    extname: '.hbs',
    partialsDir: 'views',
    layoutsDir: 'views'
};
var handlebars = express_handlebars.create( express_handlebars_config );

router.engine( 'hbs', handlebars.engine );
router.set( 'view engine', 'hbs' );
router.set( 'views', 'views' );

router.get( '/', function( req, res ){
    res.render( 'index.hbs', {} );
});

router.get( '/test', function( req, res ){
    res.render( 'test.hbs', {} );
});

router.listen( 8080 );
  1. views/index.hbs is empty
  2. views/test.hbs
<!DOCTYPE>
<html>
    <body>test.hbs</body>
</html>
  1. Start the server, try to access / or /test (these should show you the aforementioned error)
  2. Put anything in index.hbs and save it.
  3. Load / or /test (they should work now)
  4. Empty one of the views, try one of the routes, the error should happen again

@ericf
Copy link
Owner

ericf commented Apr 15, 2013

So the root issue is that in the most recent, released (1.10.0) version of Handlebar does a falsy check in its compile() method. This has been changed on master to perform a null check so that empty strings are considered valid input into the compiler.

Instead of making ExpressHandlebars handle this case, I'd rather wait until the next version of Handlebars is released which will automatically correct this. Does that work for you?

@Fauntleroy
Copy link
Author

I'll wait on the change to be released in Handlebars, then updated here. Thanks for looking into it!

@ericf
Copy link
Owner

ericf commented Apr 15, 2013

This was fixed in handlebars-lang/handlebars.js#461

@Fauntleroy
Copy link
Author

Looks like this is in Handlebars 1.0.11, so we can close this issue out.

@dchancogne
Copy link

Having issues with empty templates again.

express3-handlebars specifies the handlebars dependency as "handlebars": "1.x" and I feel this is an issue.
Up until recently installingexpress3-handlebars v0.5.0 would pull in handlebars v1.0.12 but 2 days ago handlebars released 1.1.0 (and 1.1.1) and that's the version that will now be pulled by express3-handlebars.
That latest version of handlebars fails (at least for me) on empty templates.

I feel the dependency should be specified as 1.0.x to make sure this type of issue (some dependency changing from underneath you) doesn't happen.

@ericf
Copy link
Owner

ericf commented Nov 6, 2013

I feel the dependency should be specified as 1.0.x to make sure this type of issue (some dependency changing from underneath you) doesn't happen.

I want express3-handlebars to pull down the latest version of Handlebars. You can always control which version it actually uses by supplying a value for the handlebars option

Being able to control which version of Handlebars is used was a primary design decision for this package. Add the version you want in your app's package.json and specify it like this:

// Will load the `handlebars` module specified in your app's package.json 
// instead of the version that `express3-handlebars` has in its `node_modules/` dir.
app.engine('.handlebars', exphbs({handlebars: require('handlebars')}));

@dchancogne
Copy link

@ericf Great, thanks. That makes sense. I did end up specifying the handlebars version in my package.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants