繁体   English   中英

模板路径中的通配符不适用于带有gulp的nunjucks

[英]Globbing in template path not working in nunjucks with gulp

我正在使用这个gulp插件使用nunjucks来简化HTML管理。

https://github.com/carlosl/gulp-nunjucks-render

gulp.task('default', function () {
  return gulp.src('src/templates/*.html')
    .pipe(nunjucksRender({
      path: ['src/templates/'] // String or Array
    }))
    .pipe(gulp.dest('dist'));
});

我想将模板和特定页面的部分内容保留在页面目录下的不同文件夹中,因此我尝试将路径保留为

path: ['src/templates/', 'src/common-partials/', 'src/pages/**/**/includes/']

但它不起作用。

Template render error: (unknown path)
  Error: template not found: component1.nunjucks

我的设定

在此处输入图片说明

我认为可能是两个/**/**/问题。 **模式与路径字符串中的多个级别匹配,因此我不太确定其中两个are行的行为应如何。 以下应与您的目录匹配:

'src/pages/**/includes/'

也就是说,如果nunjucks首先支持globbing(我在文档中找不到任何提及)。

path选项不支持通配符。 您必须分别传递每个路径:

gulp.task('default', function () {
  return gulp.src('src/templates/*.html')
    .pipe(nunjucksRender({
      path: ['src/templates/', 'src/pages/section_name/sub-page/includes'] 
    }))
    .pipe(gulp.dest('dist'));
});

如果您真的想使用globing,则可以使用glob模块在将每个glob传递给gulp-nunjucks-render之前对其进行gulp-nunjucks-render

var glob = require('glob');

gulp.task('default', function() {
  var includeDirs = ['src/templates/', 'src/pages/**/includes'];

  return gulp.src('src/templates/*.html')
    .pipe(nunjucksRender({
      path: includeDirs.reduce(function(dirs, g) {
        return dirs.concat(glob.sync(g));
      }, [])
    }))
    .pipe(gulp.dest('dist'));
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM