最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Interpolate new line from string in Pug - Stack Overflow

programmeradmin0浏览0评论

I am using Pug with Nodemailer in a NodeJS server to handle email templates.

In my case I have the following object in the locals object

{
    from: '[email protected]',
    message: 'a\nb\nc\nd\ne',
}

Now in the Pug file I have the following line

p #{message}

The problem is that in the email I don't get the text with new lines.

The message filed ignores the \n and puts everything on the same line a b c d e

I need to get the text in the page with the new lines as it is in the object.

a
b
c
d
e

What would be the solution for this issue?

I am using Pug with Nodemailer in a NodeJS server to handle email templates.

In my case I have the following object in the locals object

{
    from: '[email protected]',
    message: 'a\nb\nc\nd\ne',
}

Now in the Pug file I have the following line

p #{message}

The problem is that in the email I don't get the text with new lines.

The message filed ignores the \n and puts everything on the same line a b c d e

I need to get the text in the page with the new lines as it is in the object.

a
b
c
d
e

What would be the solution for this issue?

Share Improve this question edited Dec 6, 2022 at 16:29 Sean 8,2634 gold badges26 silver badges53 bronze badges asked Aug 6, 2019 at 14:09 SabbinSabbin 2,4652 gold badges21 silver badges37 bronze badges 1
  • 2 You're creating HTML, which ignores \n. You need to use <br> instead. Or use <pre> instead of <p>. Or use white-space – user5734311 Commented Aug 6, 2019 at 14:10
Add a ment  | 

2 Answers 2

Reset to default 5

As far as I know, the <p> tag ignores preformatting, maybe try using <pre> instead?

pre #{message}

If you want to use a <p> for each paragraph instead of a <pre> you can convert each newline into a new paragraph in Pug:

section
  each line in message.split('\n') 
    p #{line}

This will render:

<section>
  <p>a</p>
  <p>b</p>
  <p>c</p>
  <p>d</p>
  <p>e</p>
</section>

This assumes a, b, c, etc. in your example are actually blocks of text that you've shortened to create a quick example.

发布评论

评论列表(0)

  1. 暂无评论