6

Is it possible to use it? For example here:

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

I don't want to use - at the start of each line. If it not possible maybe there is some way to import multiline JSON file?

YakovL
  • 7,557
  • 12
  • 62
  • 102
Entry Guy
  • 425
  • 1
  • 7
  • 18
  • 1
    Unfortunately, this won't be implemented until somebody proposes a pull request: https://github.com/pugjs/pug/issues/796 – YakovL Mar 30 '18 at 22:36

2 Answers2

7

Since version 2.0.3 it is possible using this syntax:

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

Link to pull request: https://github.com/pugjs/pug/pull/1965

user2988142
  • 407
  • 3
  • 12
2

You can import JSON data during compile using LOCALS (Jade) or DATA (Pug). This is how I do it via gulpjs and Pug, movieList would be data created in the gulpfile.js and songs.json would be an external file. It's not clear from you code sample if you are using a task manager or express, etc...

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

and in the Pug Template

- 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)
Jason Lydon
  • 7,074
  • 1
  • 35
  • 43