Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added example to create partials using gulp-wrap #11

Closed
wants to merge 4 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ gulp.task('templates', function(){
});
```

## Compiling to a partial for the browser

[gulp-wrap] can be used to compile templates for the browser. Just pipe the output of gulp-handlebars to gulp-declare:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Partials aren't for just the browser only, they can be used in any Handlebars environment.
  2. The convention in this README is to wrap module names in backticks
`[gulp-wrap]` can be used to compile partials. Just pipe the output of `gulp-handlebars` to `gulp-declare`:


```javascript
var handlebars = require('gulp-handlebars');
var wrap = require('gulp-wrap');

gulp.task('templates', function() {
gulp.src(['client/templates/_*.hbs'])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment here that indicates you can use whatever glob you want to match your partials.

.pipe(handlebars())
.pipe(wrap('Handlebars.registerPartial(<%= processPartialName(file.relative) %>, Handlebars.template(<%= contents %>))', {}, {
'imports': {
'processPartialName': function(fileName) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't quote property names in object literals unless necessary

return JSON.stringify(
require('path').basename(fileName, '.js').substr(1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would require path above where the other items are required.

);
}
}
}))
.pipe(gulp.dest('build/js/'));
});
```

## API

Expand Down Expand Up @@ -75,4 +98,5 @@ Compiler options to pass to `Handlebars.precompile()`.
[npm-url]: https://npmjs.org/package/gulp-handlebars
[npm-image]: https://badge.fury.io/js/gulp-handlebars.png

[gulp-declare]: https://github.com/lazd/gulp-declare
[gulp-declare]: https://github.com/lazd/gulp-declare
[gulp-wrap]: https://github.com/adamayres/gulp-wrap