在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):lazd/gulp-handlebars开源软件地址(OpenSource Url):https://github.com/lazd/gulp-handlebars开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):gulp-handlebars
UsageInstall npm install --save-dev gulp-handlebars VERSION MISMATCH ERROR?!Are you seeing this error when using pre-compliled templates?
If you're getting a version mismatch error when using pre-compliled templates, install the handlebars version of your choice and pass it as Here's how you install your own version of handlebars:
And here's how you use that verison of handlebars in handlebars({
handlebars: require('handlebars')
}) The runtime is located in:
Compiling templates for the browser
First, install development dependencies: npm install --save-dev gulp-handlebars gulp-wrap gulp-declare gulp-concat Given the following directory structure:
To compile all templates in gulpfile.jsvar handlebars = require('gulp-handlebars');
var wrap = require('gulp-wrap');
var declare = require('gulp-declare');
var concat = require('gulp-concat');
gulp.task('templates', function(){
gulp.src('source/templates/*.hbs')
.pipe(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/'));
}); The template's filename is combined with the namespace, so the resulting this["MyApp"] = this["MyApp"] || {};
this["MyApp"]["templates"] = this["MyApp"]["templates"] || {};
this["MyApp"]["templates"]["home"] = this["MyApp"]["templates"]["home"] || {};
this["MyApp"]["templates"]["home"]["header"] = Handlebars.template(function() { /* compiled template function */ }); Namespace templates according to nested directoriesSee the namespaceByDirectory example if you'd like to compile templates with a mapping that looks like this:
Compiling to various module systemsSee the See the amd example for a full example of compiling templates to AMD modules.
Compiling partialsThe following example will precompile and register partials for all var path = require('path');
var gulp = require('gulp');
var wrap = require('gulp-wrap');
var concat = require('gulp-concat');
var handlebars = require('gulp-handlebars');
gulp.task('partials', function() {
// Assume all partials are in a folder such as source/partials/**/*.hbs
gulp.src(['source/partials/**/*.hbs'])
.pipe(handlebars())
.pipe(wrap('Handlebars.registerPartial(<%= processPartialName(file.relative) %>, Handlebars.template(<%= contents %>));', {}, {
imports: {
processPartialName: function(fileName) {
// Strip the extension and the underscore
// Escape the output with JSON.stringify
return JSON.stringify(path.basename(fileName, '.js'));
}
}
}))
.pipe(concat('partials.js'))
.pipe(gulp.dest('build/js/'));
}); See the partials example for a full example that compiles partials and templates down to a single file. Compiling using a specific Handlebars versionYou can use different versions of Handlebars by specifying the version in your package.json{
"devDependencies": {
"handlebars": "^1.3.0"
}
} gulpfile.jsgulp.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/'));
}); 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 1.x templates. The handlebars1 example copies the runtime from Compiling to separate modules for Node/BrowserifyThis example will make templates available for loading via Node's require: gulpfile.jsvar handlebars = require('gulp-handlebars');
var defineModule = require('gulp-define-module');
gulp.task('templates', function(){
gulp.src(['templates/*.hbs'])
.pipe(handlebars())
.pipe(defineModule('node'))
.pipe(gulp.dest('build/templates/'));
}); Templates can then be used within Node as such: var appTemplate = require('./build/templates/App.Header.js');
var html = appTemplate(data); Compiling to a single module for use in Node/BrowserifySee the singleModule example if you'd like to have a single module that contains all of your templates that can be used like so: yourApp.jsvar templates = require('./templates');
var output = templates.App.header(); Processing the generated template ASTThe example below removes any partial and replaces it with the text gulpfile.jshandlebars({
processAST: function(ast) {
ast.statements.forEach(function(statement, i) {
if (statement.type === 'partial') {
ast.statements[i] = { type: 'content', string: 'foo' };
}
});
}
}) Using HTMLBars with EmberSee the ember-htmlbars example for details handlebars({
handlebars: emberHandlebars,
compiler: emberTemplateCompilerFunction
}) APIhandlebars(options)options.compilerOptionsType: Compiler options to pass to options.processASTType: A function which will be passed the parsed Handlebars Abstract Syntax Tree. You can modify the AST in place or return a new AST to change the source of the precompiled template. options.handlebarsType: Handlebars library to use for precompilation. By default, the latest stable version of Handlebars is used. options.compilerType: Custom compiler function. By default, the precompile method of the provided Handlebars module is used (see options.handlebars). |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论