{"id":14986778,"url":"https://github.com/shannonmoeller/gulp-hb","last_synced_at":"2025-04-05T16:09:29.500Z","repository":{"id":17129511,"uuid":"19895778","full_name":"shannonmoeller/gulp-hb","owner":"shannonmoeller","description":"A sane Gulp plugin to compile Handlebars templates. Useful as a static site generator.","archived":false,"fork":false,"pushed_at":"2019-10-28T17:15:11.000Z","size":262,"stargazers_count":148,"open_issues_count":4,"forks_count":13,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-29T15:09:03.569Z","etag":null,"topics":["gulp","handlebars","hbs","nodejs","static-site-generator"],"latest_commit_sha":null,"homepage":"http://npm.im/gulp-hb","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/shannonmoeller.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-05-17T20:39:11.000Z","updated_at":"2024-11-23T15:32:38.000Z","dependencies_parsed_at":"2022-08-24T13:00:25.491Z","dependency_job_id":null,"html_url":"https://github.com/shannonmoeller/gulp-hb","commit_stats":null,"previous_names":[],"tags_count":53,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shannonmoeller%2Fgulp-hb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shannonmoeller%2Fgulp-hb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shannonmoeller%2Fgulp-hb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shannonmoeller%2Fgulp-hb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shannonmoeller","download_url":"https://codeload.github.com/shannonmoeller/gulp-hb/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361691,"owners_count":20926643,"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":["gulp","handlebars","hbs","nodejs","static-site-generator"],"created_at":"2024-09-24T14:13:31.632Z","updated_at":"2025-04-05T16:09:29.466Z","avatar_url":"https://github.com/shannonmoeller.png","language":"JavaScript","funding_links":[],"categories":["插件","Plugins"],"sub_categories":["模板","Templating"],"readme":"# `gulp-hb`\n\n[![NPM version][npm-img]][npm-url] [![Downloads][downloads-img]][npm-url] [![Build Status][travis-img]][travis-url] [![Coverage Status][coveralls-img]][coveralls-url] [![Tip][amazon-img]][amazon-url]\n\nA sane static [Handlebars][handlebars] Gulp plugin. Useful as a static site generator. Powered by [`handlebars-wax`][wax]. Think [Assemble][assemble], but with a lot less [Jekyll][jekyll] baggage.\n\nTo precompile templates into JavaScript, see [`gulp-handlebars`][gulp-handlebars].\n\n## Install\n\n```console\n$ npm install --save-dev gulp-hb\n```\n\n## Usage\n\n```js\nconst gulp = require('gulp');\nconst hb = require('gulp-hb');\n\n// Basic\n\nfunction basic() {\n    return gulp\n        .src('./src/{,posts/}*.html')\n        .pipe(hb()\n            .partials('./src/assets/partials/**/*.hbs')\n            .helpers('./src/assets/helpers/*.js')\n            .data('./src/assets/data/**/*.{js,json}')\n        )\n        .pipe(gulp.dest('./web'));\n}\n\ngulp.task('basic', basic);\n\n// Advanced\n\nfunction advanced() {\n    const hbStream = hb({ debug: true })\n        // Partials\n        .partials('./partials/components/**/*.{hbs,js}')\n        .partials('./partials/layouts/**/*.{hbs,js}')\n        .partials({\n            boo: '{{#each boo}}{{greet}}{{/each}}',\n            far: '{{#each far}}{{length}}{{/each}}'\n        })\n\n        // Helpers\n        .helpers(require('handlebars-layouts'))\n        .helpers('./helpers/**/*.js')\n        .helpers({\n            foo: function () { ... },\n            bar: function () { ... }\n        })\n\n        // Decorators\n        .decorators('./decorators/**/*.js')\n        .decorators({\n            baz: function () { ... },\n            qux: function () { ... }\n        })\n\n        // Data\n        .data('./data/**/*.{js,json}')\n        .data({\n            lorem: 'dolor',\n            ipsum: 'sit amet'\n        });\n\n    return gulp\n        .src('./src/{,posts/}*.html')\n        .pipe(hbStream)\n        .pipe(gulp.dest('./web'));\n}\n\ngulp.task('advanced', advanced);\n```\n\n### Template Context\n\nThe template context is a merge of pre-registered data and file-specific data. Pre-registered data is available to all templates and is set using the `.data()` method. File-specific data is available to the current template and is set via the [`file.data`](#file-specific-data-sources) property.\n\n#### `@` Data Variables\n\n##### @root\n\nThe merged object of pre-registered data and file-specific data is available as the primary context of your templates. In cases where accessing this data would require the use of `../`, you may access the top-level context via `@root`:\n\n```handlebars\n{{ foo }}\n{{ @root.foo }}\n\n{{#each bar}}\n    {{ ../foo }}\n    {{ @root.foo }}\n{{/each}}\n```\n\n##### @global\n\nIn cases where file-specific data keys collide with pre-registered data keys, you may access the pre-registered data via `@global`:\n\n```handlebars\n{{ @global.foo }}\n```\n\n##### @local\n\nIn cases where pre-registered data should be ignored, you may access the file-specific data via `@local`.\n\n```handlebars\n{{ @local.foo }}\n```\n\n##### @file\n\nIn cases where information about the template file itself is needed, you may access the [file object][file] via `@file`:\n\n```handlebars\n{{ @file.path }}\n```\n\n#### File-specific Data Sources\n\nFile-specific data is set via the `file.data` property using other plugins such as [`gulp-data`][gulp-data], [`gulp-data-json`][gulp-data-json], or [`gulp-front-matter`][gulp-front-matter].\n\n```js\nconst gulp = require('gulp');\nconst data = require('gulp-data');\nconst frontMatter = require('gulp-front-matter');\nconst hb = require('gulp-hb');\n\nfunction inject() {\n    return gulp\n        .src('./src/*.html')\n\n        // Load an associated JSON file per post.\n        .pipe(data((file) =\u003e {\n            return require(file.path.replace('.html', '.json'));\n        }))\n\n        // Parse front matter from post file.\n        .pipe(frontMatter({\n            property: 'data.frontMatter'\n        }))\n\n        // Data for everyone.\n        .pipe(hb().data('./data/**/*.js'))\n\n        .pipe(gulp.dest('./web'));\n}\n\ngulp.task('inject', inject);\n```\n\n#### Multiple Data Sources\n\nMultiple data sources can be used to render the same set of templates to different directories using [`through2`][through2].\n\n```js\nconst gulp = require('gulp');\nconst hb = require('gulp-hb');\nconst through = require('through2');\n\nfunction i18n() {\n    return gulp\n        .src('./i18n/*.json')\n        .pipe(through.obj((file, enc, cb) =\u003e {\n            const locale = file.stem;\n            const data = {\n                locale: locale,\n                i18n: JSON.parse(file.contents.toString())\n            };\n\n            gulp\n                .src('./templates/**/*.html')\n                .pipe(hb().data(data))\n                .pipe(gulp.dest('./dist/' + locale))\n                .on('error', cb)\n                .on('end', cb);\n        }));\n}\n\ngulp.task('i18n', i18n);\n```\n\n## API\n\n### `hb([options]): TransformStream`\n\n- `options` `{Object}` (optional) Passed directly to [`handlebars-wax`][wax] so check there for more options.\n  - `bustCache` `{Boolean}` (default: `true`) Force reload data, partials, helpers, and decorators.\n  - `cwd` `{String}` (default: `process.cwd()`) Current working directory.\n  - `debug` `{Boolean|Number}` (default: `false` or `0`) Whether to log registered functions and data (`true` or `1`) and glob parsing (`2`).\n  - `handlebars` `{Handlebars}` (optional) A specific instance of Handlebars, if needed.\n  - `compileOptions` `{Object}` Options to use when compiling templates.\n  - `templateOptions` `{Object}` Options to use when rendering templates.\n  - `partials` `{String|Array.\u003cString\u003e|Object|Function(handlebars)}`\n  - `parsePartialName` `{Function(options, file): String}`\n  - `helpers` `{String|Array.\u003cString\u003e|Object|Function(handlebars)}`\n  - `parseHelperName` `{Function(options, file): String}`\n  - `decorators` `{String|Array.\u003cString\u003e|Object|Function(handlebars)}`\n  - `parseDecoratorName` `{Function(options, file): String}`\n  - `data` `{String|Array.\u003cString\u003e|Object}`\n  - `parseDataName` `{Function(options, file): String}`\n\nReturns a Gulp-compatible transform stream that compiles [Handlebars][handlebars] templates to static output.\n\n### .partials(pattern [, options]): TransformStream\n\n- `pattern` `{String|Array\u003cString\u003e|Object|Function(handlebars)}`\n- `options` `{Object}` Same options as `hb()`.\n\nLoads additional partials. See [`handlebars-wax`][wax].\n\n### .helpers(pattern [, options]): TransformStream\n\n- `pattern` `{String|Array\u003cString\u003e|Object|Function(handlebars)}`\n- `options` `{Object}` Same options as `hb()`.\n\nLoads additional helpers. See [`handlebars-wax`][wax].\n\n### .decorators(pattern [, options]): TransformStream\n\n- `pattern` `{String|Array\u003cString\u003e|Object|Function(handlebars)}`\n- `options` `{Object}` Same options as `hb()`.\n\nLoads additional decorators. See [`handlebars-wax`][wax].\n\n### .data(pattern [, options]): TransformStream\n\n- `pattern` `{String|Array\u003cString\u003e|Object}`\n- `options` `{Object}` Same options as `hb()`.\n\nLoads additional data. See [`handlebars-wax`][wax].\n\n## Contribute\n\nStandards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors.\n\n### Test\n\n```console\n$ npm test\n```\n\n----\n\nMIT © [Shannon Moeller](http://shannonmoeller.com)\n\n[assemble]: http://assemble.io/\n[context]: https://github.com/shannonmoeller/handlebars-wax#context-and-rendering\n[file]: https://github.com/gulpjs/vinyl#file\n[gulp-data]: https://github.com/colynb/gulp-data#usage\n[gulp-data-json]: https://github.com/kflorence/gulp-data-json#example\n[gulp-front-matter]: https://github.com/lmtm/gulp-front-matter#usage\n[gulp-handlebars]: https://github.com/lazd/gulp-handlebars#usage\n[handlebars]: https://github.com/wycats/handlebars.js#usage\n[jekyll]: https://jekyllrb.com/\n[through2]: https://github.com/rvagg/through2#api\n[wax]: https://github.com/shannonmoeller/handlebars-wax#usage\n\n[amazon-img]:    https://img.shields.io/badge/amazon-tip_jar-yellow.svg?style=flat-square\n[amazon-url]:    https://www.amazon.com/gp/registry/wishlist/1VQM9ID04YPC5?sort=universal-price\n[coveralls-img]: http://img.shields.io/coveralls/shannonmoeller/gulp-hb/master.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/shannonmoeller/gulp-hb\n[downloads-img]: http://img.shields.io/npm/dm/gulp-hb.svg?style=flat-square\n[npm-img]:       http://img.shields.io/npm/v/gulp-hb.svg?style=flat-square\n[npm-url]:       https://npmjs.org/package/gulp-hb\n[travis-img]:    http://img.shields.io/travis/shannonmoeller/gulp-hb/master.svg?style=flat-square\n[travis-url]:    https://travis-ci.org/shannonmoeller/gulp-hb\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshannonmoeller%2Fgulp-hb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshannonmoeller%2Fgulp-hb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshannonmoeller%2Fgulp-hb/lists"}