{"id":13513095,"url":"https://github.com/pillarjs/hbs","last_synced_at":"2025-05-13T22:09:58.180Z","repository":{"id":1087934,"uuid":"939810","full_name":"pillarjs/hbs","owner":"pillarjs","description":"Express view engine wrapper for Handlebars","archived":false,"fork":false,"pushed_at":"2024-06-13T15:54:14.000Z","size":224,"stargazers_count":1668,"open_issues_count":7,"forks_count":203,"subscribers_count":41,"default_branch":"master","last_synced_at":"2025-04-29T21:39:25.186Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pillarjs.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"express"}},"created_at":"2010-09-26T06:23:45.000Z","updated_at":"2025-04-21T18:11:45.000Z","dependencies_parsed_at":"2024-11-25T18:08:39.849Z","dependency_job_id":"c2915b23-387e-4ed1-8e8a-bd668e9680dc","html_url":"https://github.com/pillarjs/hbs","commit_stats":{"total_commits":310,"total_committers":29,"mean_commits":"10.689655172413794","dds":0.4903225806451613,"last_synced_commit":"5790e5e1debc3990ee17b9cb26edfb95ac75ae41"},"previous_names":["donpark/hbs"],"tags_count":49,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Fhbs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Fhbs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Fhbs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pillarjs%2Fhbs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pillarjs","download_url":"https://codeload.github.com/pillarjs/hbs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254036831,"owners_count":22003654,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T04:00:37.716Z","updated_at":"2025-05-13T22:09:53.172Z","avatar_url":"https://github.com/pillarjs.png","language":"JavaScript","readme":"# hbs\n\n[![NPM Version][npm-version-image]][npm-url]\n[![NPM Downloads][npm-downloads-image]][npm-url]\n[![Linux Build][github-actions-ci-image]][github-actions-ci-url]\n[![Windows Build][appveyor-image]][appveyor-url]\n[![Test Coverage][coveralls-image]][coveralls-url]\n\n[Express.js](https://expressjs.com/) view engine for\n[handlebars.js](https://handlebarsjs.com/)\n\n## Installation\n\nThis is a [Node.js](https://nodejs.org/en/) module available through the\n[npm registry](https://www.npmjs.com/). Installation is done using the\n[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):\n\n```bash\n$ npm install hbs\n```\n\n## Use ##\n\nUsing *hbs* as the default view engine requires just one line of code in your app setup. This will render `.hbs` files when `res.render` is called.\n\n```javascript\napp.set('view engine', 'hbs');\n```\n\nTo use a different extension (i.e. html) for your template files:\n\n```javascript\napp.set('view engine', 'html');\napp.engine('html', require('hbs').__express);\n```\n\n## Helpers and Partials ##\n\nhbs exposes the `registerHelper` and `registerPartial` method from handlebars.\n\n```javascript\nvar hbs = require('hbs');\n\nhbs.registerHelper('helper_name', function (options) { return 'helper value'; });\nhbs.registerPartial('partial_name', 'partial value');\n```\n\nFor convenience, `registerPartials` provides a quick way to load all partials from a specific directory:\n\n```javascript\nvar hbs = require('hbs');\n\nhbs.registerPartials(__dirname + '/views/partials', function (err) {});\n```\n\nPartials that are loaded from a directory are named based on their filename, where spaces and hyphens are replaced with an underscore character:\n\n```\ntemplate.html      -\u003e {{\u003e template}}\ntemplate 2.html    -\u003e {{\u003e template_2}}\nlogin view.hbs     -\u003e {{\u003e login_view}}\ntemplate-file.html -\u003e {{\u003e template_file}}\n```\n\nSee the [handlebars.js documentation](https://handlebarsjs.com/) for more\ninformation.\n\nThe way the file is renamed to a partial name can be adjusted by providing a `rename` option. The function will recieve the file path relative to the registered directory and without the file extension. If the returned value contains any whitespace, those characters are replaced with a corresponding underscore character.\n\n```js\nvar hbs = require('hbs')\n\nhbs.registerPartials(path.join(__dirname, '/views/partials'), {\n  rename: function (name) {\n    // all non-word characters replaced with underscores\n    return name.replace(/\\W/g, '_')\n  }\n})\n```\n\n**Note:** This method is async; meaning that the directory is walked in a non-blocking manner to app startup.\n\n## Exposing locals as template data ##\n\nhbs has the ability to expose the application and request locals within any context inside a view. To enable this functionality, simply call the `localsAsTemplateData` method and pass in your Express application instance.\n\n```javascript\nvar hbs = require('hbs');\nvar express = require('express');\n\nvar app = express();\nhbs.localsAsTemplateData(app);\n\napp.locals.foo = \"bar\";\n```\n\nThe local data can then be accessed using the `@property` syntax:\n\n```\ntop level: {{@foo}}\n{{#each items}}\n  {{label}}: {{@foo}}\n{{/each}}\n```\nNote: In partials and templates, local data can be accessed without using `@` prefix.\n\n## handlebars ##\n\nThe handlebars require used by hbs can be accessed via the `handlebars` property on the `hbs` module.\n\nIf you wish to use handlebars methods like `SafeString` please do so on this property. Do not register helpers or partials in this way.\n\n```\n// hbs.handlebars is the handlebars module\nhbs.handlebars === require('handlebars');\n```\n\n## Recipes ##\n\n### more than one instance ###\nYou can create isolated instances of hbs using the `create()` function on the module object.\n\n```\nvar hbs = require('hbs');\n\nvar instance1 = hbs.create();\nvar instance2 = hbs.create();\n\napp.engine('html', instance1.__express);\napp.engine('hbs', instance2.__express);\n```\n\nEach instance has the same methods/properties as the `hbs` module object. The module object is actually just an instance created for you automatically.\n\n### extra scripts or styles ##\nSometimes it is useful to have custom scripts or stylesheets on your pages. Handlebars does not provide a way to import or extend a template, but through the use of helpers you can create a similar result.\n\nWe can take advantage of the fact that our body template is processed before the layout template. Knowing this, we can create two helpers `block` and `extend` which can be used to 'inject' custom stylesheets or scripts into the layout template. The `block` helper will act as a placeholder for values specified in earlier `extend` helpers.\n\nSee examples/extend for a working example. Note how the index.hbs file defines extra stylesheets and scripts to be injected into the layout. They are put into the head section and at the end of the body respectively. If this was not done, the stylesheet would be in the body and the script would print `foo bar` too soon.\n\n## Helpful Modules ##\n\n- **[hbs-utils](https://www.npmjs.com/package/hbs-utils)**: A small utility\n  library that provides helpers for registering and compiling partials\n  including automatic updates when partials are changed.\n\n[appveyor-image]: https://badgen.net/appveyor/ci/dougwilson/nodejs-hbs/master?label=windows\n[appveyor-url]: https://ci.appveyor.com/project/dougwilson/nodejs-hbs\n[coveralls-image]: https://badgen.net/coveralls/c/github/pillarjs/hbs/master\n[coveralls-url]: https://coveralls.io/r/pillarjs/hbs?branch=master\n[github-actions-ci-image]: https://badgen.net/github/checks/pillarjs/hbs/master?label=linux\n[github-actions-ci-url]: https://github.com/pillarjs/hbs/actions?query=workflow%3Aci\n[node-image]: https://badgen.net/npm/node/hbs\n[node-url]: https://nodejs.org/en/download/\n[npm-downloads-image]: https://badgen.net/npm/dm/hbs\n[npm-url]: https://npmjs.org/package/hbs\n[npm-version-image]: https://badgen.net/npm/v/hbs\n","funding_links":["https://opencollective.com/express"],"categories":["Libraries","Repository","JavaScript","NodeJS fullstack :tada: :tada: :tada:"],"sub_categories":["Templating","Express view engine :palm_tree: :palm_tree: :palm_tree:"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpillarjs%2Fhbs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpillarjs%2Fhbs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpillarjs%2Fhbs/lists"}