【问题标题】:Globbing in template path not working in nunjucks with gulp模板路径中的通配符不适用于 gulp 的 nunjucks
【发布时间】:2016-09-29 10:44:18
【问题描述】:

我正在使用这个 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

我的设置

【问题讨论】:

    标签: node.js gulp template-engine nunjucks


    【解决方案1】:

    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'));
    });
    

    如果你真的想使用 glob,你可以使用 glob 模块来解析每个 glob,然后再将其传递给 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'));
    });
    

    【讨论】:

      【解决方案2】:

      我认为这两个/**/**/ 可能是问题所在。 ** 模式匹配路径字符串中的多个级别,所以我不确定其中两个在行中的行为应该是什么。以下内容应与您的目录匹配:

      'src/pages/**/includes/'
      

      也就是说,如果 nunjucks 一开始就支持通配符(我在文档中找不到任何提及)。

      【讨论】:

      • src/pages/**/includes/ 也不起作用,同样的错误
      • 你能尝试一条更简单的通配路径吗?例如src/**。在这种情况下,也许 nunjucks 真的不支持通配符。
      猜你喜欢
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 2021-05-26
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 2015-10-06
      相关资源
      最近更新 更多