Ent*_*Guy 4 html web pugjs pug
可以使用吗?例如这里:
- var movieList = [{title: "Ocean's Eleven", rating: 9.2}, {title: "Pirates of the Caribbean", rating: 9.7}];
mixin movie-card(movie)
h2.movie-title= movie.title
div.rating
p= movie.rating
for movie in movieList
+movie-card(movie)
Run Code Online (Sandbox Code Playgroud)
我不想-在每一行的开头使用。如果不可能的话,也许有某种方法可以导入多行JSON文件?
从2.0.3版开始,可以使用以下语法:
-
var arr = ["Very", "Long",
"Array"];
Run Code Online (Sandbox Code Playgroud)
链接到拉取请求:https : //github.com/pugjs/pug/pull/1965
您可以在编译期间使用 LOCALS (Jade) 或 DATA (Pug)导入 JSON 数据。这就是我通过 gulpjs 和 Pug 实现的方法,movieList 将是在 gulpfile.js 中创建的数据,songs.json 将是外部文件。从代码示例中不清楚您是否使用任务管理器或 Express 等......
gulpfile.js
var fs = require('fs'),
gulp = require('gulp'),
pug = require('gulp-pug'),
movieList = [{title: "Ocean's Eleven", rating: 9.2}, {title: "Pirates of the Caribbean", rating: 9.7}];
gulp.task('markup', function() {
gulp.src('./markup/*.pug')
.pipe(pug({
data: {
// in Jade, this would be "locals: {"
"movies": movieList,
"music": JSON.parse( fs.readFileSync('./songs.json', { encoding: 'utf8' }) )
}
)
.pipe(gulp.dest('../'))
});
});
Run Code Online (Sandbox Code Playgroud)
并在哈巴狗模板中
- var movieList = locals['movies'] // assuming this will eventually be "= data['movies']"
- var musicList = locals['music'] // assuming this will eventually be "= data['music']"
mixin movie-card(movie)
h2.movie-title= movie.title
div.rating
p= movie.rating
for movie in movieList
+movie-card(movie)
mixin song-card(song)
h2.song-title #{song.title}
div.rating
p #{song.rating}
for song in musicList
+song-card(song)
Run Code Online (Sandbox Code Playgroud)