post by lucperkins on Apr 16, 2021

I’m working on a project right now that makes heavy use of code samples. What I’d like to be able to do is store those samples as strings (I have a complex generation script that makes those strings…long story) and have Hugo respect newlines in those strings. Here’s a very basic example:

# data/samples.yaml samples: - "foo/nbar"

What I’d like to be able to do is to make those strings respect the newline, like so:

foo bar

Is there a Hugo function that does this? I’ve tried a bunch of them but none have worked thus far.

post by jmooring on Apr 16, 2021

post by chrillek on Apr 17, 2021

There’s no newline here, just a slash and an n. Newlines are escaped n, thus
\n

post by zwbetz on Apr 18, 2021

Building off the replies of others…

Given data/samples.yaml

samples: # Will literally show this string - foo\nbar # Will respect newlines - | foo bar

Then this template

{{ range site.Data.samples.samples }} {{ . }} {{ end }}

Will output this

foo\nbar foo bar

post by gethugothemes on Apr 18, 2021

Try this, it will also support other HTML or MD tag like **Bold**, <i>italic<i>

data:

# data/samples.yaml samples: - "foo<br>bar"

template:

{{ range site.Data.samples.samples }} {{ . |markdownify}} {{ end }}

config:

[markup.goldmark.renderer] unsafe = true

post by lucperkins on Apr 20, 2021

Okay, actually I mischaracterized the issue. The issue is that I need to generate strings with newlines and have Hugo respect those newlines. I was confusing two things I was working on, derp. Something like this:

{{ $key := "my_key" }} {{ $value := "some string" }} {{ $concatenated := printf `[%s]\nvalue = "%s"` $key $value }} {{ $tomlExample := highlight $concatenated "toml" "" }} <pre> <code> {{ $tomlExample }} </code> </pre>

I want the output to look like this (note that it’s on multiple lines):

[my_key] value = "some string"

It’s kind of a silly example but it illustrates what I’m after.

post by zwbetz on Apr 20, 2021

The syntax if off a bit.

Change this:

`[%s]\nvalue = "%s"`

To this:

"[%s]\nvalue = \"%s\""

post by lucperkins on Apr 21, 2021

@zwbetz Unfortunately Hugo just gives the raw string inside of <code>. I’m not sure there’s a great way directly around this. I’ve come up with a workaround that involves avoiding string interpolation entirely and I’m satisfied with it :rofl:Thanks for having a look!

post by zwbetz on Apr 21, 2021

Hmm. It rendered correctly on my end.

Well, glad to hear you found another solution.

2 years later

post by ltdat26 on Sep 27, 2023

In my case, I’m using replace to push the work for safeHTML.

{{ $content := "This is a line of text.\nAnd this is on a new line." }} {{ $content = replace $content "\n" "<br>" | safeHTML }} {{ $content | safeHTML }}

The output will be:
This is a line of text.<br>And this is on a new line.

This way, I can include literal “\n” characters and have them treated as line breaks in your HTML content when using safeHTML .

Oooh, sorry, I’m 2 years late :scream:


Powered by Discourse