Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline variables in Pug (ex-Jade)

Tags:

html

web

pug

pugjs

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?

like image 357
Entry Guy Avatar asked Apr 19 '16 00:04

Entry Guy


People also ask

What are variables in Pug?

Variables in pug. There are 4 types of data available in pug which can be stored in pug variables which are explained below : String Variable : Variable stores String value as shown below : //example - 1 var str = "String"; //example - 2 var value = 'This is string type of data' ; Integer Variable : Variable stores Integer value as shown below :

Can I use interpolation in Pug/Jade?

Previous versions of Pug/Jade supported an interpolation syntax such as: a(href="/#{url}")Link This syntax is no longer supported. Alternatives are found below. (Check our migration guidefor more information on other incompatibilities between Pug v2 and previous versions.)

What is the difference between a pug and a jade file?

Pug files are marked with ; Jade files are marked with . Make sure the Pug (ex-Jade) and File Watchers required plugins are enabled on the Settings/Preferences | Plugins page, tab Installed, see Managing plugins for details.

What is Pug Jade template engine?

Pug (Jade) Template Engine. WebStorm integrates with the Pug (Jade) template engine. Before you start, make sure you have Node.js on your computer. The Pug (ex-Jade) plugin introduces the following changes to the WebStorm UI: The Jade file item is added to the menu. The Pug files are marked with ; the Jade files are marked with .


Video Answer


2 Answers

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

like image 148
user2988142 Avatar answered Nov 15 '22 03:11

user2988142


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)
like image 45
Jason Lydon Avatar answered Nov 15 '22 03:11

Jason Lydon