a-h / templ

A language for writing HTML user interfaces in Go.
https://templ.guide/
MIT License
8.04k stars 264 forks source link

fmt: <pre> tags not handled correctly #782

Closed firefart closed 3 months ago

firefart commented 3 months ago

Currently <pre> tags seem to be handled incorrectly.

1) templ fmt removes all double newlines and leading and trailing spaces. Text rendered inside

 tags should be rendered "as is" so double newlines and whitespaces are most likely expected here.

Example:

Before:

templ Details() {
    <pre>
        Test 13
lmafsfd

        sdf dsfdsfdsf

        sdfsd
        sdfsdf
                                  sdfsdfsdfsd
    </pre>
}

After:

templ Details() {
    <pre>
        Test 13
        lmafsfd
        sdf dsfdsfdsf
        sdfsd
        sdfsdf
        sdfsdfsdfsd
    </pre>
}

2) The render process seems to remove all whitespaces by minifying the content. Currentl output of above example:

<pre>Test 13 lmafsfd sdf dsfdsfdsf sdfsd sdfsdf sdfsdfsdfsd</pre>

This completely breaks rendering inside pre tags within templ. Is there a way to exclude pre tags from fmt and the minify process?

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre

joerdav commented 3 months ago

Hi there is a solution for this now that should do the trick for you:

templ x() {
    <pre> {`
        package main

        func main() {
            println("Hello, world!")
        }
    `} </pre>
}
firefart commented 3 months ago

Hi there is a solution for this now that should do the trick for you:

templ x() {
  <pre> {`
      package main

      func main() {
          println("Hello, world!")
      }
  `} </pre>
}

Thanks for the hint! Is there a nice way to use and output variables inside this format and keep it somewhat readable?

joerdav commented 3 months ago

Yes, I would probably reach for the fmt package:

templ x(text string) {
    <pre> {fmt.Sprintf(`
        package main

        func main() {
            println("%s")
        }
    `, text)} </pre>
}