【问题标题】:Multiline variables in Pug (ex-Jade)Pug 中的多行变量(前 Jade)
【发布时间】:2018-09-11 16:19:42
【问题描述】:

可以用吗?例如这里:

- 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 文件?

【问题讨论】:

标签: html web pug pugjs


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    您可以在compile 期间使用 LOCALS (Jade) 或 DATA (Pug) 导入 JSON 数据。这就是我通过 gulpjs 和 Pug 执行此操作的方式,movieList 将是在 gulpfile.js 中创建的数据,而 song.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('../'))
      });
    });
    

    在 Pug 模板中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 2016-10-28
      • 2020-04-16
      • 2018-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多