2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Express + JadeでHTMLを見やすく出力する方法

Last updated at Posted at 2013-11-22

Express+JadeでHTMLを出力すると改行無しで1行で出力されます。

<!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express</p></body></html>

res.render()の第二引数にpretty: trueを渡すと改行+インデントされます。

// routes.index.js
exports.index = function(req, res){
  res.render('index', { title: 'Express', pretty: true });
};
<!DOCTYPE html>
<html>
  <head>
    <title>Express</title>
    <link rel="stylesheet" href="/stylesheets/style.css">
  </head>
  <body>
    <h1>Express</h1>
    <p>Welcome to Express</p>
  </body>
</html>

これは、res.render(view, [locals], callback)jade.rendeFile(filename, options, callback)を呼んでいるためです。この第二引数optionsは、

API Documentation : Jade - Template Engine

options
An options object (see above), also used as the locals object

と、Jadeのオプションとテンプレートに渡す変数の両方の目的で使われています。Jadeのオプションには他にも以下のものがあります。

  • filename
  • pretty
  • self
  • debug
  • compileDebug
  • compiler
  • globals

なので、例えばテンプレート変数名のつもりでうっかりdebugとか使うと、動かなくはないけどコンソールに大量にログが出力されます。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?