gulp-ejs官網(wǎng)上有如此使用示例:javascriptvarejs=require("gulp-ejs");gulp.src("./templates/*.ejs").pipe(ejs({msg:"HelloGulp!"})).pipe(gulp.dest("./dist"));會得到如此效果:htmlHelloGulp!出于某種需要,我把需要的ejs參數(shù)(data)放在了一個外部文件中:json//config.json{"content_includes":{"title":"Hello"}}htmljavascript//gulpfile.jsvargulp=require('gulp');vargutil=require('gulp-util');varejs=require('gulp-ejs');varrename=require('gulp-rename');varpath=require('path');varcfg=require('./config.json');vartmp={}gulp.task('compile',function(){gulp.src("./html/**/[^_]*.ejs").pipe(rename(function(path){varbasename=path.basename;//判斷相應的key是否存在if(cfg.hasOwnProperty(basename)){//若存在則緩存這個key的value,即需要傳給ejs的參數(shù)tmp=cfg[basename];}else{tmp={};}})).pipe(ejs(tmp)).on('error',gutil.log).pipe(rename(function(path){path.extname=".html";})).pipe(gulp.dest('./dest'));});結(jié)果,提示參數(shù)未傳入:titleisnotdefined請問應該怎么做?
如何從外部引入?yún)?shù)傳給gulp-ejs?
梵蒂岡之花
2019-04-21 20:42:16