76

I'm pretty sure that this is a no-brainer but I didn't find any snippet of sample code. What's the best way to insert line breaks (aka the good ol' br/)?

As far as I can see if I put a "br" at the beginning of an empty line, it is rendered as <br/> but if I have to show several lines of text, the resulting code is quite verbose:

.poem 
 p 
 | Si chiamava Tatiana, la sorella... 
 br 
 | Noi siamo i primi, almeno lo crediamo
 br 
 | Che un tale nome arditamente nella
 br 
 | Cornice d’un romanzo introduciamo.
 br 
 | E che dunque? E’ piacevole, sonoro.
 br 
 | Lo so che a molti privo di decoro 
 br 
 | Apparirà, già fuori moda, e degno
 br 
 | Piuttosto d’un ancella, certo segno, 
 br 
 | confessiamolo pur senza paura,
 br 
 | di quanto s’è noialtri al gusto avversi
 br 
 | nei nostri nomi (a non parlar di versi). |br
 br 
 | Credemmo conquistare la cultura,
 br 
 | e non ne abbiamo preso, in conclusione,
 br 
 | che la ricerca dell’affettazione.

Is there a better way to solve this? (incidentally I'm asking for the same thing with the image tag...)

royhowie
11.2k14 gold badges54 silver badges67 bronze badges
asked Mar 25, 2011 at 14:43
2
  • I was going to get around to asking this, thank you +1 Commented Mar 25, 2011 at 14:47
  • MDN suggests caution using the line break element. developer.mozilla.org/en-US/docs/Web/HTML/Element/… I like the white-space: pre solution! Commented Jan 17, 2021 at 3:29

12 Answers 12

61

The cleanest and easiest solution is to use the style attribute white-space: pre; eg:

.poem 
 p(style='white-space:pre;')
 | Si chiamava Tatiana, la sorella... 
 | Noi siamo i primi, almeno lo crediamo
 | Che un tale nome arditamente nella
 | Cornice d’un romanzo introduciamo.
 | E che dunque? E’ piacevole, sonoro.
 | Lo so che a molti privo di decoro 
 | Apparirà, già fuori moda, e degno
 | Piuttosto d’un ancella, certo segno, 
 | confessiamolo pur senza paura,
 | di quanto s’è noialtri al gusto avversi
 | nei nostri nomi (a non parlar di versi). |br
 | Credemmo conquistare la cultura,
 | e non ne abbiamo preso, in conclusione,
 | che la ricerca dell’affettazione.
answered Jul 19, 2011 at 19:10
Sign up to request clarification or add additional context in comments.

5 Comments

I agree. It looks like the white-space rule has good browser support, as well. Thanks!
@JGallardo what's the best way, then? By the way, I'd use whitespace: pre-wrap instead, to avoid the content overflow it's container
I would use whitespace: pre-wrap -or- whitespace: pre-line
This doesn't seem to work here in 2016 :/ I'm using Pug in node and the whitespace is being removed.
Prefer pre-line. There's a comparison table here
53

I figured it out. Just go ahead and throw a good ol' fashioned <br /> tag in there. You'll be golden : )

p
 |hey this is my <br />
 |broken paragraph!

UPDATE: Jade now supports using just br for line breaks.

random_user_name
26.2k7 gold badges81 silver badges119 bronze badges
answered Mar 25, 2011 at 23:07

6 Comments

the pipes aren't needed in the current version of jade
@generalhenry: Actually, I'm working with a xml doctype and, if I don't use pipes, the first word of the line is used as tag, AFAIK.
+1 This is the cleanest solution, but as stated by Matteo, the pipes are needed!
pretty sure there needs to be a space after those pipes, or you could add a period: p. and skip the pipes in the indented content
There is no space necessary after the pipe. At least not in Jade 1.3.1
|
8

This doesn't seem to be an answer, so:

You can also do it by adding inline br tags using Jade/Pug's inline tag format, e.g.:

p.
 Some text on the first line.#[br]
 Some text on the second line.

Produces:

 <p>Some text on the first line.<br>
 Some text on the second line.
 </p>
answered Dec 11, 2016 at 23:19

1 Comment

Thanks .. I was looking for a break to work among html tags
7

so that you're aware.. if you're pulling this information.. say from an SQL database where you've already manually entered in line breaks (say in a textarea of a form) you can do the following on the server to your output

var contentParse = function(content){
 content = content.replace(/\n?\r\n/g, '<br />' );
 return content;
};

and then in jade use

!{content}

the ! lets jade know that you're inserting raw html into the variable you're trying to render out

source: https://github.com/visionmedia/jade#tag-text

answered Apr 17, 2013 at 7:57

2 Comments

That's fine until the content contains some actual HTML, e.g. <script src=.....>. If you are inserting raw HTML, then you should probably escape special HTML chars, such as < > and &. Jade's runtime.js has an escape() function which appears to escape some of these.
I think that it should be something like div!= content in the Jade (Pug?) template.
5

robustly with a div per line:

p.poem 
 .line Si chiamava Tatiana, la sorella... 
 .line Noi siamo i primi, almeno lo crediamo
 .line Che un tale nome arditamente nella
 .line Cornice d’un romanzo introduciamo.
 .line E che dunque? E’ piacevole, sonoro.
 .line Lo so che a molti privo di decoro 
 .line Apparirà, già fuori moda, e degno
 .line Piuttosto d’un ancella, certo segno, 
 .line confessiamolo pur senza paura,
 .line di quanto s’è noialtri al gusto avversi
 .line nei nostri nomi (a non parlar di versi).
 .line Credemmo conquistare la cultura,
 .line e non ne abbiamo preso, in conclusione,
 .line che la ricerca dell’affettazione.

or simply with a pre:

style pre.poem { font-family:ariel }

pre.poem 
 Si chiamava Tatiana, la sorella... 
 Noi siamo i primi, almeno lo crediamo
 Che un tale nome arditamente nella
 Cornice d’un romanzo introduciamo.
 E che dunque? E’ piacevole, sonoro.
 Lo so che a molti privo di decoro 
 Apparirà, già fuori moda, e degno
 Piuttosto d’un ancella, certo segno, 
 confessiamolo pur senza paura,
 di quanto s’è noialtri al gusto avversi
 nei nostri nomi (a non parlar di versi). 
 Credemmo conquistare la cultura,
 e non ne abbiamo preso, in conclusione,
 che la ricerca dell’affettazione.
answered Mar 25, 2011 at 21:55

1 Comment

not to nitpick, but according to a strict doctype, p tags can only contain inline elements, so your first example may not pass validation, depending on the doctype.
0

I was able to do the following after @haxxxton

app.use(function(req, res, next){
 var contentParse = function (content){
 content = content.replace(/\n?\r\n/g, '<br />' );
 return content;
 };
 res.locals.contentParse = contentParse;
 next();
});

For example, it can be used in a jade template using the function p!= contentParse(post.description)

answered Jan 6, 2014 at 21:37

1 Comment

oh no no no. This is far too complicated
0

this also works well.

div
 pre
 | this is line 1
 | this is line 2
answered Apr 17, 2017 at 16:02

Comments

0

Give your paragraph a style to keep the newlines and end the p line with a dot:

.poem 
 p(style="white-space: pre-line;").
 Si chiamava Tatiana, la sorella... 
 Noi siamo i primi, almeno lo crediamo
 Che un tale nome arditamente nella
 Cornice d’un romanzo introduciamo.
 E che dunque? E’ piacevole, sonoro.
 Lo so che a molti privo di decoro 
 Apparirà, già fuori moda, e degno
 Piuttosto d’un ancella, certo segno, 
 confessiamolo pur senza paura,
 di quanto s’è noialtri al gusto avversi
 nei nostri nomi (a non parlar di versi).
 Credemmo conquistare la cultura,
 e non ne abbiamo preso, in conclusione,
 che la ricerca dell’affettazione.
answered Aug 16, 2018 at 11:50

Comments

0

I was generating a SASS file from PUG template and I needed each item on a new line.
This is what worked for me:

//- custom-variables.pug
//- GENERATE COLORS
each color, idx in colors
 | #{idx}: #{color};
 |
const pug = require("pug");
const colors = {
 $primary: "#0074d9",
 $secondary: "#ff4136",
 $green: "green",
};
// Compile the source code
const compiledFunction = pug.compileFile("./scripts/custom-variables.pug");
console.log(compiledFunction({ colors }));
// outputs:
/*
$primary: #0074d9;
$secondary: #ff4136;
$green: green;
*/
answered Apr 18, 2021 at 11:05

Comments

0
.poem: pre(style="font-family: unset").
 Si chiamava Tatiana, la sorella... 
 Noi siamo i primi, almeno lo crediamo
 Che un tale nome arditamente nella
 Cornice d’un romanzo introduciamo.
 E che dunque? E’ piacevole, sonoro.
 Lo so che a molti privo di decoro 
 Apparirà, già fuori moda, e degno
 Piuttosto d’un ancella, certo segno, 
 confessiamolo pur senza paura,
 di quanto s’è noialtri al gusto avversi
 nei nostri nomi (a non parlar di versi).
 Credemmo conquistare la cultura,
 e non ne abbiamo preso, in conclusione,
 che la ricerca dell’affettazione.

or

.poem: p(style="white-space: pre-wrap").
 Si chiamava Tatiana, la sorella... 
 Noi siamo i primi, almeno lo crediamo
 Che un tale nome arditamente nella
 Cornice d’un romanzo introduciamo.
 E che dunque? E’ piacevole, sonoro.
 Lo so che a molti privo di decoro 
 Apparirà, già fuori moda, e degno
 Piuttosto d’un ancella, certo segno, 
 confessiamolo pur senza paura,
 di quanto s’è noialtri al gusto avversi
 nei nostri nomi (a non parlar di versi).
 Credemmo conquistare la cultura,
 e non ne abbiamo preso, in conclusione,
 che la ricerca dell’affettazione.
answered Jan 24, 2022 at 17:01

1 Comment

A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it.
-1

Just in case you have not used the year filter on the first search: How to add br tag with Jade HTML

Put the text on a new line with a preceding |:

p first line
br
| second line
answered Jun 2, 2016 at 10:10

Comments

-3

Try this:

- for(var i = 0; i < 10; i++)
 | hello
 | world
answered Jul 8, 2015 at 18:03

1 Comment

Thank you for posting an answer to this question! Code-only answers are discouraged on Stack Overflow, because a code dump with no context doesn't explain how or why the solution will work, making it difficult for the original poster (or any future readers) to understand the logic behind it. Please, edit your question and include an explanation of your code so that others can benefit from your answer. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.