express-hbs-compile
v1.0.1
Published
Get HTML string compiled by express-hbs.
Downloads
20
Maintainers
Readme
express-hbs-compile
Get HTML string compiled by express-hbs.
Click here to see the change log.
Installation
npm install --save express-hbs-compile
API
hbs()
Create render function with options.
const hbs = require('express-hbs-compile');
const render = hbs({
viewsDir: path.join(__dirname, 'views'),
partialsDir: path.join(__dirname, 'views/partials'),
layoutsDir: path.join(__dirname, 'views/layout'),
defaultLayout: path.join(__dirname, 'views/layout/default.hbs'),
extname: '.hbs',
contentHelperName: 'contentFor',
blockHelperName: 'block',
// helpers: {
// sayhello: name => `Hello, ${name}`,
// }
});
Parameters
- {string} viewsDir The absolute directory path of the template. This option is required.
- {string|string[]} partialsDir? Directory absolute path of the partial template. One or more directories can be set. Default is "path.join(options.viewsDir, 'partials')".
- {string} layoutsDir? Directory absolute path of the layout template. Default is "path.join(options.viewsDir, 'layout')".
- {string} defaultLayout? Absolute path of the default layout file. Default is "path.join(options.layoutsDir, 'default.hbs')".
- {string} extname? Extension for layout and partial templates. Default is
.hbs
". - {string} contentHelperName? Override the 'contentFor' helper name used in the template.
- {string} blockHelperName? Override the 'block' helper name used in the template.
- {{[key: string]: Handlebars.HelperDelegate}} helpers A helper accessible from the template. The key is the helper name and the value is the object that will be the helper function.
Return value
{(filePath: string, data?: object) => Promise<string>} Returns the render function.
Exceptions
- {TypeError} Throws an exception if the viewsDir option is unset.
- {TypeError} Throws an exception if the directory specified by the viewsDir option is not found.
- {TypeError} Throws an exception if the directory specified by the partialsDir option is not found.
- {TypeError} Throws an exception if the directory specified by the layoutsDir option is not found.
- {TypeError} Throws an exception if the file specified by the defaultLayout option is not found.
render()
Receive the result of compiling the template as a string.
The handlebars used internally are extended and can use custom helpers found here.
const hbs = require('express-hbs-compile');
const render = hbs({
viewsDir: path.join(__dirname, 'views'),
});
// Compile template.
const html = await render('index.hbs', {name: 'foo'});
Parameters
- {string} template File name or absolute path of the template. If only the file name is specified, the template is searched from the directory specified by the viewsDir option.
- {object} data? Objects to be expanded on the template. settings, cache, and layout are reserved words and cannot be used as key names for data.
Return value
{Promise<string>} Compiled HTML string.
Exceptions
- {TypeError} Throws an exception if any of the data keys contain reserved words (settings, cache, layout).
- {TypeError} Throws an exception if the template file cannot be found.
Testing
With npm do:
npm test
Author
Takuya Motoshima