Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Change how layout paths are resolved #11

Merged
merged 2 commits into from

2 participants

@autoric

Currently, layout paths are resolved relative the template. This can become a pain for nested views and maintainability. Given an example like:

views/
    index.hbs
    home/
        userpage.hbs
    admin/
        users/
            list.hbs
    layouts/
        default.hbs

These views would respectively use the layout directives. For a lot of views this can be frustrating if I decide I want to refactor the directory structure or whatever.

{{!< layouts/default }}
{{!< ../layouts/default }}
{{!< ../../layouts/default }}

This pull request will add a configuration option "layoutsDir" that all layouts will be resolved against. If that option is not given, it will fallback to current behavior.

@mgutz mgutz merged commit d598656 into from
@mgutz
Owner

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 16, 2013
  1. @autoric
  2. @autoric

    updated readme

    autoric authored
This page is out of date. Refresh to see the latest.
View
7 README.md
@@ -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.
@@ -49,7 +50,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}}
@@ -99,7 +100,7 @@ File `views/layout/default.hbs`
File `views/index.hbs`
```
-{{!< layout/default}}
+{{!< default}}
{{#contentFor "pageStyles"}}
<style>
View
6 example/app.js
@@ -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');
View
4 example/views/fruits/index.hbs
@@ -1,5 +1,5 @@
-<!-- relative path to layout -->
-{{!< ../layout/fruits}}
+<!-- name of layout -->
+{{!< fruits}}
{{#contentFor "pageStyles"}}
<style>
View
10 lib/hbs.js
@@ -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 {
Something went wrong with that request. Please try again.