Giter Site home page Giter Site logo

Comments (7)

diamondburned avatar diamondburned commented on June 18, 2024

I found a workaround in the meantime:

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

		func main() &#123;
			println("Hello, world!")
		&#125;
	`} </pre>
}

from templ.

a-h avatar a-h commented on June 18, 2024

Just thinking about this some more, now that the auto formatting, and spaces between variables are dealt with.

Options as I see it from here:

  • Accept that this workaround is acceptable and document it.
  • Decide that children of the <pre> element should not have whitespace removed.
  • Something else?

What are you thoughts?

from templ.

a-h avatar a-h commented on June 18, 2024

For option 2 above, the solution would probably be something like how the indentation for autoformatting now works. The element type has a field on it to set the behaviour:

templ/parser/v2/types.go

Lines 319 to 325 in fa536dd

type Element struct {
Name string
Attributes []Attribute
IndentAttrs bool
Children []Node
IndentChildren bool
}

So in this case, adding a new PreserveWhitespace field to the element, and setting it to true if the element name is pre. Then, adding something to the generator that adds a context variable and passes that down to children. The generated code would always need to include the whitespace for elements, and optionally include it in the output based on the context value.

It could potentially slow down all rendered content, due to needing to check for the flag.

from templ.

brlbil avatar brlbil commented on June 18, 2024

I have just discovered the library, great job, thanks. I have to say I was a bit surprised when I realized the output did not have the same structure as the source. I think it would be much easier to reason about it if they were the same.

What happens in the case of putting an element inside another element like pre, I do not think a workaround works with that.

For example

<pre>
  for _, item := range items {
    <a href={ templ.URL(item.Name) }>{ item.Name }</a>   { item.ModifiedAt }    { item.Size }
  }
</pre>

Also, if this library will ever handle text formats other than HTML one day, auto line collapsing and single spacing would not be acceptable.

How about making single space, new line collapsing optional?

from templ.

a-h avatar a-h commented on June 18, 2024

Thanks for everyone's thoughts on this.

One solution might be to drop built in minification as a goal, and suggest the use of something like github.com/tdewolff/Minify which provides HTTP middleware.

This may negatively affect the performance for users that require it.

However, it would simplify the job of templ since the rendering logic would be simpler.

On the other hand, maybe it's as simple as updating the rendering logic to not strip white space from pre elements. With regards to supporting changes to block/inline driven by CSS, I note that the minfication library above doesn't support that.

Supporting formats other than HTML isn't a goal for templ.

The goal is to make Go a viable language for commercial front end web development when used with tools like HTMX.

I'll digest this thread and come up with some concrete actions.

from templ.

diamondburned avatar diamondburned commented on June 18, 2024

This may negatively affect the performance for users that require it.

I think this is a fair tradeoff. It should be reasonable for people to assume that templ is a templating library, not some sort of framework that tries to do everything at once.

Of course, we could always just introduce a config file, though I'm personally not a huge fan of this idea.

from templ.

joerdav avatar joerdav commented on June 18, 2024

This could do with some investigation to find out if the issue still stands, and make a decision on the outcome.

from templ.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.