【问题标题】:Gulp-compile-handlebars for multiple html pages?多个html页面的Gulp-compile-handlebars?
【发布时间】:2014-10-15 05:15:10
【问题描述】:

到目前为止,我只有两个 gulp 任务,例如 gulp.task('handlebars-index')gulp.task('handlebars-about')。以下是文档中的代码,https://www.npmjs.org/package/gulp-compile-handlebars

我不确定如何处理两个 .handlebars 文件的任务。

var gulp = require('gulp');
var handlebars = require('gulp-compile-handlebars');
var rename = require('gulp-rename');

gulp.task('handlebars', function () {
    var templateData = {
        firstName: 'Kaanon'
    },
    options = {
        ignorePartials: true, //ignores the unknown footer2 partial in the handlebars template, defaults to false
        partials : {
            footer : '<footer>the end</footer>'
        },
        batch : ['./src/partials'],
        helpers : {
            capitals : function(str){
                return str.toUpperCase();
            }
        }
    }

    // here how do I add an index.html and say and about.html?
    return gulp.src('src/index.handlebars')
        .pipe(handlebars(templateData, options))
        .pipe(rename('index.html'))
        .pipe(gulp.dest('dist'));
});

你可以看到上面的任务基本上是获取 index.handlebars 然后编译它并创建一个 index.html 文件。

如果我添加了一系列车把文件,任务将如何知道如何创建 .html 版本?

    return gulp.src(['src/index.handlebars','src/about.handlebars'])
        .pipe(handlebars(templateData, options))
        .pipe(rename('index.html'))
        .pipe(rename('about.html'))
        .pipe(gulp.dest('dist'));

上述方法显然行不通。

【问题讨论】:

    标签: javascript gulp handlebars.js


    【解决方案1】:

    Gulp-rename 还具有一个功能,您可以只更改部分路径。

    return gulp.src('src/*.handlebars')
        .pipe(handlebars(templateData, options))
        .pipe(rename(function(path) {
            path.extname = '.html';
        }))
        .pipe(gulp.dest('dist'));
    

    https://github.com/hparra/gulp-rename#usage

    【讨论】:

    • 好的,看起来不错,谢谢,当然胜过拥有 3-4 个 gulp 任务,哈哈。我应该更多地关注 Gulp-rename 插件,我正在搜索 gulp-compile-handlebars 文档。谢谢你摇滚!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2013-06-05
    相关资源
    最近更新 更多