gohighlight.js

How can I render PRE-formatted content using a-h/templ in Go?


I want to utilise highlight.js in a a-h/templ template, but I can't get the pre block to format correctly.

The goal is to have:

<pre>
  <code class="language-go">
    var User struct {
      ID int
    }
  </code>
</pre>

in the templ file, but I'm having the following problems:

  1. templ can't process the above snippet, due to it not escaping the {. If I switch the { to (, it renders.
  2. When it does render, templ is removing the newlines, and rendering it as var User struct ( ID int ) on a single line

I've tried @templ.Raw, but that doesn't resolve either issue (doesn't escape the {, and can't handle line breaks)

Is there a way to render pre-formatted content like this, do do I need to look for a different solution?


Solution

  • After going through the docs page by page, and answer was in Expressions > Escaping

    This works nicely:

    <pre>
        <code class="language-go">
            { `type User struct {
                ID int
            }` }
        </code>
    </pre>