Permalink
Browse files

Merge pull request #11 from autoric/master

Change how layout paths are resolved
  • Loading branch information...
2 parents b13d298 + ffd4d31 commit d598656f5ed9bbebb754dc4087a90b78e700ea9c @mgutz mgutz committed Jan 16, 2013
Showing with 20 additions and 7 deletions.
  1. +4 −3 README.md
  2. +5 −1 example/app.js
  3. +2 −2 example/views/fruits/index.hbs
  4. +9 −1 lib/hbs.js
View
@@ -23,7 +23,8 @@ Options for `#express3`
defaultLayout: "{String} [Optional] Absolute path to default layout template",
extname: "{String} Extension for templates, defaults to `.hbs`",
handlebars: "{Module} Use external handlebars instead of express-hbs dependency",
- partialsDir: "{String} Path to partials templates"
+ partialsDir: "{String} Path to partials templates",
+ layoutsDir: "{String} Path to layout templates"
});
Partials may use any extension, which is better for syntax highlighting.
@@ -48,7 +49,7 @@ To define block content in a page.
There are three ways to use a layout, listed in the order in which they are checked for and used:
-1. Declarative within a page. Use handlebars comment. `LAYOUT` is a relative path from template.
+1. Declarative within a page. Use handlebars comment. If you have declared a layoutsDir in the configuration, `LAYOUT` is a relative path from layoutsDir. Otherwise, `LAYOUT` is a relative path from the template.
{{!< LAYOUT}}
@@ -98,7 +99,7 @@ File `views/layout/default.hbs`
File `views/index.hbs`
```
-{{!< layout/default}}
+{{!< default}}
{{#contentFor "pageStyles"}}
<style>
View
@@ -7,7 +7,11 @@ var hbs = require('..'); // should be `require('express-hbs')` outside of this e
app.use(express.static(__dirname + '/public'));
// Hook in express-hbs and tell it where partials are found
-app.engine('hbs', hbs.express3({partialsDir: __dirname + '/views/partials', defaultLayout: __dirname + '/views/layout/default.hbs'}));
+app.engine('hbs', hbs.express3({
+ partialsDir: __dirname + '/views/partials',
+ layoutsDir: __dirname + '/views/layout',
+ defaultLayout: __dirname + '/views/layout/default.hbs'
+}));
app.set('view engine', 'hbs');
app.set('views', __dirname + '/views');
@@ -1,5 +1,5 @@
-<!-- relative path to layout -->
-{{!< ../layout/fruits}}
+<!-- name of layout -->
+{{!< fruits}}
{{#contentFor "pageStyles"}}
<style>
View
@@ -25,6 +25,11 @@ var blocks = {};
var partialsDir;
/**
+ * Absolute path to the layouts directory
+ */
+var layoutsDir;
+
+/**
* Keep copy of options configuration.
*/
var _options;
@@ -128,6 +133,7 @@ function cachePartials() {
* handlebars: "override handlebars",
* defaultLayout: "path to default layout",
* partialsDir: "absolute path to partials",
+ * layoutsDir: "absolute path to the layouts",
* extname: "extension to use"
* }
*
@@ -144,6 +150,8 @@ exports.express3 = function(options) {
partialsDir = _options.partialsDir;
if (partialsDir) cachePartials();
+ layoutsDir = _options.layoutsDir;
+
return _express3;
};
@@ -196,7 +204,7 @@ var _express3 = function(filename, options, cb) {
var layout = matches[1];
// cacheLayout expects absolute path
- layout = path.resolve(path.join(path.dirname(filename), layout));
+ layout = path.resolve(path.join(layoutsDir?layoutsDir:path.dirname(filename), layout));
cacheLayout(layout, options.cache, cb);
}
else {

0 comments on commit d598656

Please sign in to comment.