gulp-nunjucks-render
gulp-nunjucks-render copied to clipboard
How do I get an error when entering data incorrectly?
I have a config using plumber, but if I try to call data in my templates that does not exist in my data.json, then an empty string will just get into html, is it possible to catch an error when data is called incorrectly?
My config
function html() {
let data = {};
fs.readdirSync(paths.src.data).map((item) => {
if (path.extname(item).indexOf('.json') > -1) {
data = {...data, ...JSON.parse(fs.readFileSync(path.resolve(paths.src.data, item)))};
}
});
return src([
`${paths.src.html}/pages/**/*.*(njk|html|nunjucks)`,
`!${paths.src.html}/pages/**/_*.*(njk|html|nunjucks)`,
])
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(nunjucksRender({
path: paths.src.html,
ext: config.html.extname,
data: validateJSON(data),
inheritExtension: false,
envOptions: {
watch: false,
},
manageEnv: (env) => {
// Filters
Object.keys(nunjucksFilters).forEach((name) => {
if (name.indexOf('esModule') > -1) {
return;
}
env.addFilter(name, nunjucksFilters[name]);
});
// Global functions
Object.keys(nunjucksFunctions).forEach((name) => {
if (name.indexOf('esModule') > -1) {
return;
}
env.addGlobal(name, nunjucksFunctions[name]);
});
},
loaders: null
}))
.pipe(dest(paths.build.html))
.pipe(
gulpIf(config.html.inject.css,
inject(
src(`${paths.build.styles}/**/*.css`, {read: false}), {
relative: true,
addPrefix: config.html.inject.prefix,
})
)
)
.pipe(gulpIf(!isDev(), htmlmin({
collapseWhitespace: true, // удаляем все переносы
conservativeCollapse: true, // сворачиваем все в 1 строку
removeComments: true // удаляем все комментарии
})))
.pipe(dest(paths.build.html))
.pipe(reload({stream: true}))
}
Json
{
"data": {
"en": {
"title": "Hello, bro",
}
}
}
Code example
<h2>{{ data[lang].header.title }}</h2>
-> <h2>Hello, bro</h2>
but
<h2>{{ data[lang].header.tite }}</h2>
-> <h2></h2>
I would like to get an error if .tite
does not exist