Skip to content

Commit

Permalink
Added handlebars3 example, fixed bad link in handlebars1 example
Browse files Browse the repository at this point in the history
  • Loading branch information
lazd committed Mar 3, 2015
1 parent 53a68c8 commit c1749dd
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/handlebars1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ This example will show you how to compile templates using a specific version of

## Dependencies

* [`[email protected]`](https://www.npmjs.org/package/gulp-define-module) - An older version of handlebars
* [`[email protected]`](https://www.npmjs.org/package/handlebars) - An older version of handlebars
* [`gulp-declare`](https://www.npmjs.org/package/gulp-declare) - Declare properties and sub-properties
* [`gulp-concat`](https://www.npmjs.org/package/gulp-concat) - Combine output into a single file
* [`gulp-wrap`](https://www.npmjs.org/package/gulp-wrap) - Add `require()` and `Handlebars.template()` statements
Expand Down
1 change: 1 addition & 0 deletions examples/handlebars3/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
79 changes: 79 additions & 0 deletions examples/handlebars3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Compile templates against Handlebars 3.x

This example will show you how to compile templates using a specific version of Handlebars.

**The runtime you include on the client side MUST match the version you compile templates with.** You cannot use the the 2.x runtime with 3.x templates. This example copies the runtime from `node_modules/handlebars/dist/handlebars.runtime.js` and uses that on the client side. Follow a similar pattern in your application to keep the runtime up to date with the compiler.

## Dependencies

* [`[email protected]`](https://www.npmjs.org/package/handlebars) - The latest version of handlebars
* [`gulp-declare`](https://www.npmjs.org/package/gulp-declare) - Declare properties and sub-properties
* [`gulp-concat`](https://www.npmjs.org/package/gulp-concat) - Combine output into a single file
* [`gulp-wrap`](https://www.npmjs.org/package/gulp-wrap) - Add `require()` and `Handlebars.template()` statements

## Running the example

Type the following commands from the root of this repository:

```
npm install # install the plugin's dependencies
cd examples/handlebars3
npm install # install Handlebars 3.x locally
gulp
open build/index.html
```
You should see the following output:

```js
This is the app!
```

## Usage

#### 1. Install development dependencies:

```shell
# Note the specific version of Handlebars is specified here
npm install --save-dev gulp-handlebars gulp-wrap gulp-declare gulp-concat [email protected]
```

#### 2. Add the `require()` statements and `template` task to your gulpfile

```js
var gulp = require('gulp');
var wrap = require('gulp-wrap');
var declare = require('gulp-declare');
var concat = require('gulp-concat');
var handlebars = require('gulp-handlebars');

gulp.task('templates', function(){
gulp.src('source/templates/*.hbs')
.pipe(handlebars({
// Pass your local handlebars version
handlebars: require('handlebars')
}))
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
namespace: 'MyApp.templates',
noRedeclare: true // Avoid duplicate declarations
}))
.pipe(concat('templates.js'))
.pipe(gulp.dest('build/js/'));
});

```

#### 3. Include the `build/js/templates.js` file in your application
```html
<script src="js/templates.js"></script>
```

You may also concatenate into your build output if you like. See [`gulp-concat`](https://www.npmjs.org/package/gulp-concat) for more info.

#### 4. Access templates according to their name:
```html
<script>
// This will render the template defined by App.header.hbs
document.querySelector('#app').innerHTML = MyApp.templates.App();
</script>
```
31 changes: 31 additions & 0 deletions examples/handlebars3/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var gulp = require('gulp');
var wrap = require('gulp-wrap');
var declare = require('gulp-declare');
var concat = require('gulp-concat');
/** REMOVE ME **/ var handlebars = require('../../');
/** USE ME **/ // var handlebars = require('gulp-handlebars');

gulp.task('templates', function(){
gulp.src('source/templates/*.hbs')
.pipe(handlebars({
handlebars: require('handlebars')
}))
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
namespace: 'MyApp.templates',
noRedeclare: true // Avoid duplicate declarations
}))
.pipe(concat('templates.js'))
.pipe(gulp.dest('build/js/'));
});

gulp.task('copy', function(){
gulp.src('node_modules/handlebars/dist/handlebars.runtime.js')
.pipe(gulp.dest('build/js/'));

gulp.src('source/index.html')
.pipe(gulp.dest('build/'));
});

// Default task
gulp.task('default', ['copy', 'templates']);
9 changes: 9 additions & 0 deletions examples/handlebars3/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "gulp-handlebars-handlebars3",
"description": "An example of using Handlebars 3.x with gulp-handlebars",
"version": "0.0.0",
"main": "gulpfile.js",
"devDependencies": {
"handlebars": "^3.0.0"
}
}
15 changes: 15 additions & 0 deletions examples/handlebars3/source/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<title>Handlebars Test</title>
<script src="js/handlebars.runtime.js"></script>
<script src="js/templates.js"></script>
</head>
<body>
<div id="app"></div>
<script>
// This will render the template defined by App.hbs
document.querySelector('#app').innerHTML = MyApp.templates.App();
</script>
</body>
</html>
1 change: 1 addition & 0 deletions examples/handlebars3/source/templates/App.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is the app!

0 comments on commit c1749dd

Please sign in to comment.