-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added handlebars3 example, fixed bad link in handlebars1 example
- Loading branch information
Showing
7 changed files
with
137 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is the app! |