tera icon indicating copy to clipboard operation
tera copied to clipboard

What is the proper way to insert line break in expressions?

Open qianbinbin opened this issue 4 years ago • 6 comments

I was writing a zola shortcode to highlight codes from file, like this:

{% set source = load_data(path="path/to/hello.c") %}
{{ "```c\n" ~ source ~ "\n```" | markdown | safe }}

But the \n doesn't work, just appears as "\n" rather than line break. What is the proper way to implement this? Thanks!

qianbinbin avatar Apr 16 '21 06:04 qianbinbin

Hm try to put the | safe before the | markdown as well?

Keats avatar Jul 03 '21 18:07 Keats

Unfortunately that doesn't seem to work:

<pre>{{ "\n" | safe }}</pre>
<pre>{{ "\n" | safe | markdown }}</pre>
<pre>{{ "\n" | safe | markdown | safe }}</pre>

Results in

<pre>\n</pre>
<pre>&lt;p&gt;\n&lt;/p&gt;</pre>
<pre><p>\n</p></pre>

As you can see, the \n is never converted to a newline.

I have a similar case to the OP: I want to create a shortcode that will generate the backticks so that it can include syntax highlighting.

acheronfail avatar Aug 26 '21 11:08 acheronfail

Note that I've found a workaround: replace \n with a literal newline, eg:

<pre>{{ "hello" ~ "
" ~ "world" }}</pre>

Not the nicest, but hey it works.

Related: https://github.com/Keats/tera/issues/663

acheronfail avatar Aug 29 '21 11:08 acheronfail

Probably too much escaping going on.

Keats avatar Aug 30 '21 10:08 Keats

Just hit the same issue trying to run - {{ outputs | join(sep="\n -") }}

svallory avatar Mar 05 '24 17:03 svallory

Yeah it's not working in the current version. v2 has it working already

Keats avatar Mar 06 '24 19:03 Keats