【问题标题】:Preserving newlines in Jade在 Jade 中保留换行符
【发布时间】:2012-11-24 12:01:47
【问题描述】:

每当我渲染一个 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

【问题讨论】:

    标签: node.js pug


    【解决方案1】:

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

    【讨论】:

    • 我用的是 Express 的 Jade,所以我做了 "app.set('view options', { pretty: true });"然而,就在“.set('view engine', 'jade');”之后,我仍然可以在一行中获得所有 HTML。我将我的 Jade 模板添加到问题中。
    • 您使用的是哪个版本的 express?在 express 3 中,我认为您必须将其从 app.locals app.locals.pretty = true 更改。检查这个github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x
    • 啊,这些令人沮丧的版本...我 2 天前才开始使用 NodeJS,我已经不得不“迁移”...无论如何谢谢!
    猜你喜欢
    • 2013-07-27
    • 2013-11-10
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    相关资源
    最近更新 更多