Answer the question
In order to leave comments, you need to log in
How to take data from .json file and iterate it in Jade?
In general, there is an array of data:
{
"navigationData": [
{
"name": "Link for display",
"url": "http://google.com"
},
{
"name": "Looooong long items for test long plain text",
"url": "http://google.com"
},
{
"name": "Link for display",
"url": "http://google.com"
},
{
"name": "Looooong long items for test long plain text",
"url": "http://google.com"
},
{
"name": "Link for display",
"url": "http://google.com"
},
{
"name": "Looooong long items for test long plain text",
"url": "http://google.com"
},
{
"name": "Link for display",
"url": "http://google.com"
},
{
"name": "Looooong long items for test long plain text",
"url": "http://google.com"
},
{
"name": "Link for display",
"url": "http://google.com"
},
{
"name": "Looooong long items for test long plain text",
"url": "http://google.com"
},
{
"name": "Link for display",
"url": "http://google.com"
},
{
"name": "Looooong long items for test long plain text",
"url": "http://google.com"
},
{
"name": "Link for display",
"url": "http://google.com"
},
{
"name": "Looooong long items for test long plain text",
"url": "http://google.com"
},
{
"name": "Link for display",
"url": "http://google.com"
},
{
"name": "Looooong long items for test long plain text",
"url": "http://google.com"
}
]
}
let gulp = require('gulp'),
browserSync = require('browser-sync').create(),
autoprefixer = require('gulp-autoprefixer'),
concat = require('gulp-concat'),
sass = require('gulp-sass'),
cleancss = require('gulp-clean-css'),
jade = require('gulp-jade'),
minify = require('gulp-minify'),
data = require('gulp-data'),
path = require('path'),
fs = require('fs')
...
gulp.task('jade', function(done) {
let LOCALS = {}
gulp.src('app/*.jade')
.pipe(data( function(file) {
return JSON.parse(
fs.readFileSync('./app/data/data.json')
)
}))
.pipe(jade({
locals: LOCALS,
pretty: true
}))
.pipe(gulp.dest('app'))
done()
})
...
nav.navbar-list
- var nav = navigationData
ul
each val in nav
li.navbar-list-item
a(href=val.url)=val.name
events.js:167
throw er; // Unhandled 'error' event
^
TypeError: C:\Users\elliz\OneDrive\Документы\Работа\Тестовые задания\alef\app\index.jade:18
16| - var nav = navigationData
17| ul
> 18| each val in nav
19| li.navbar-list-item
20| a(href=val.url)=val.name
21|
Cannot read property 'length' of undefined
Answer the question
In order to leave comments, you need to log in
Contrary to all the documentation in gulp-jade and gulp-data, it turned out that you need to do this:
gulp.task('jade', function(done) {
gulp.src('app/*.jade')
.pipe( data(file => require('./app/data/data.json')))
.pipe(jade({
pretty: true
}))
.pipe(gulp.dest('app'))
done()
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question