Pug中的多行变量(ex-Jade)

时间:2016-04-19 00:09:15

标签: html web pug pugjs

可以使用它吗?例如:

- 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)

我不想在每行的开头使用-。 如果不可能,有可能导入多行JSON文件?

2 个答案:

答案 0 :(得分:3)

从版本2.0.3开始,可以使用以下语法:

-
  var arr = ["Very", "Long",
             "Array"];

链接到拉取请求:https://github.com/pugjs/pug/pull/1965

答案 1 :(得分:2)

您可以使用LOCALS(Jade)或DATA(Pug)在compile期间导入JSON数据。这是我通过gulpjs和Pug做的方式,movieList将是在gulpfile.js中创建的数据,songs.json将是一个外部文件。如果您正在使用任务管理器或快递等,那么您的代码示例并不清楚......

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

和帕格模板

- 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)