0

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
  • 1000 views, several good solutions to a real and common problem, and zero upvotes. Oh, Stack Overflow. Commented Jun 11 at 17:04

3 Answers 3

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>
1
  • I tried this, since the documentation mentions it. Unfortunately, it seems to have no effect in the current version of Pug. The label and input tags are still stuck together with no space between them. Commented May 5, 2019 at 5:31
1

In Pug Template, a space between the elements can be added by including a PIPELINE like this- | after an element and putting whitespace after the pipeline character.

Example-

span.price 200
| //- Whitespace included
span.person per person

Pipeline character allows to include content outside of the element. I hope this answer will be helpful for someone.

0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.