在模板路径中的玻璃不在nunjucks中用吞咽的方式工作 [英] Globbing in template path not working in nunjucks with gulp

查看:280
本文介绍了在模板路径中的玻璃不在nunjucks中用吞咽的方式工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

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

如果您真的想要使用globbing,您可以使用之前解析每个glob。gulp -c <模块名> <模块名> <模块名> <模块名> <模块名> <模块名> nunjucks-render :

  var glob = require('glob'); 
$ b $ 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){
返回dirs.concat(glob.sync(g));
},[])
}))
.pipe(gulp.dest('dist'));
});


I'm using this gulp plugin to use nunjucks to make HTML management easier.

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

I want to keep my templates and partials of specific pages under page's directory in different folders so I tried this to keep the path as

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

but it's not working.

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

My setup

解决方案

The path option doesn't support globbing. You have to pass each path individually:

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

If you really want to use globbing you can use the glob module to resolve each glob before passing it to 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'));
});

这篇关于在模板路径中的玻璃不在nunjucks中用吞咽的方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆