npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-hbs-compile

v1.0.1

Published

Get HTML string compiled by express-hbs.

Downloads

7

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

License

MIT