在Jade中保留换行符

4
每当我渲染一个JADE模板时,所有HTML都会在一行中显示。这使得在视图源代码模式下阅读起来很困难。我该如何告诉JADE创建正确缩进的HTML呢?
以下是我的模板代码:
#application
  p#docs
    a(href='/docs/index.html') Documentation

  p#user-input
    input#msg(name='msg', size='50')
    input#submit(name='submit', type='submit', value='Send a Message')

  ul#messages
1个回答

5
在Jade的编译选项中将pretty设置为true。可以通过多种方式来完成,具体取决于您如何进行编译:
  • 从命令行传递-P--pretty标志。
  • 从express 3.x: app.locals.pretty = true;

(express 2.x使用不同的语法:app.set('view options', { pretty: true });,请参见迁移指南:https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x)

然后您可以执行以下操作:
#test.     // <-- notice the dot
    Lorem Ipsum is simply dummy text of 
    the printing and typesetting industry. 
    Lorem Ipsum has been the industry's standard dummy 
    text ever since the 1500s ,
    when an unknown printer took a galley of type and scrambled 

这将产生

<div id="test">
    Lorem Ipsum is simply dummy text of 
    the printing and typesetting industry. 
    Lorem Ipsum has been the industry's standard dummy 
    text ever since the 1500s ,
    when an unknown printer took a galley of type and scrambled 
</div>

1
我在Express中使用Jade,所以在".set('view engine', 'jade');"之后加入了"app.set('view options', { pretty: true });",但是我仍然得到所有HTML都在一行中的结果。我将我的Jade模板添加到了问题中。 - Erel Segal-Halevi
4
你正在使用哪个版本的Express?我认为在Express 3中,你需要将它从app.locals改为app.locals.pretty = true。请查看https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x - jaime
啊,这些令人沮丧的版本问题...我才刚开始使用NodeJS两天,现在就不得不“迁移”了...无论如何还是谢谢! - Erel Segal-Halevi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接