-
-
Notifications
You must be signed in to change notification settings - Fork 284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can i use templ to generate text instead of html ? #385
Comments
Why would you use it in that way? |
Hi @ymolists - sorry to disappoint, but there's no plan to support the generation of plain text (or YAML, or anything else) with templ templates. The vision of templ is purely to...
But... if you wanted to abuse templ to do it, you could, by stripping the HTML tags and getting the plain text. Obviously, there are various risks with this, and the performance isn't going to be as good, since you're parsing the HTML. However, it still might be faster than the built in Go templates. With this template: package main
templ Header() {
<div>This is a header</div>
}
templ Footer() {
<div>This is a footer</div>
}
templ TextTemplate(content string) {
@Header()
<p>
{ content }
</p>
@Footer()
} And this code... package main
import (
"context"
"fmt"
"log"
"strings"
"github.com/k3a/html2text"
)
func main() {
ctx := context.Background()
w := new(strings.Builder)
err := TextTemplate("Put this & this text into your template").Render(ctx, w)
if err != nil {
log.Fatalf("failed to render: %v", err)
}
s := html2text.HTML2Text(w.String())
fmt.Println(s)
} You get the output:
I'm not willing to support this sort of thing, since it's a bit of a niche use case, but it's possible. |
The point is there are amny tools that only generate text. tmpl provides a way that is type safe and that also has lsp support. Right now everyone is forced to use go templates which is a really bad option for all sorts of reasons the mnimum being not being type safe and cumbersome. I hope i dont have to convince any that that go templating is terrible ! temple seems like a really nice template framework that could be the de facto text/code generation instead of go template because its based on generated code. The hardest part here i guess is the lsp integration. |
I'm going to close this, since I think the question is answered, and it's not an issue with templ. |
Hi folks
I am wondering if it is possible to replace all my go template based code with templ. I am hoping that this is already supported. I imagine that this could be a usecase many people would want. As in we have the equivalent of html/text templates in the go standard library.
Regards
The text was updated successfully, but these errors were encountered: