How do I prevent Pug from collapsing all whitespace between two tags?

回眸只為那壹抹淺笑 提交于 2019-12-13 23:33:06

问题


When I do this in a template:

div(class="field")
    label Password
    input(type="password")

I get this output:

<div class="field"><label>Password</label><input type="password"></div>

So the label and input tags have zero space between them, instead of the single space between them that they would have in a normal HTML file where they're on two different lines.

How do I get that standard single space between the two elements, in a Pug template, in the least ugly possible way? I could use CSS to fix the spacing, but that seems like a lot of verbosity to add, just to get what is a standard HTML feature when you aren't using Pug.


回答1:


The Pug documentation demonstrates how to control whitespace using pipe characters.

div(class="field")
    |
    |
    label Password
    |
    |
    input(type="password")

This should render:

<div class="field">
    <label>Password</label>
    <input type="password"/>
</div>



回答2:


This does what I want/expect (put exactly one space between the two elements, in the rendered HTML output).

div(class="field")
    label Password
    = ' '
    input(type="password")

Using the = operator at the beginning of a line allows you to insert a Javascript expression. So here, I'm inserting a string containing a single space.



来源:https://stackoverflow.com/questions/55961434/how-do-i-prevent-pug-from-collapsing-all-whitespace-between-two-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!