Closed ainsleyclark closed 1 week ago
There's a feature in the next release that will allow you to write script tags that contain JSON: https://github.com/a-h/templ/blob/main/jsonscript.go
But, I notice that it doesn't have a way to override the "type" attribute to ld+json
, so I'll add that.
So, in the next release, you'll be able to do:
if props.Org != nil {
@templ.JSONScript(props.Org).WithType("application/ld+json")
}
Amazing thank you @a-h!
Added in https://github.com/a-h/templ/commit/94c1028906535eba5b8a33c2c5babd82ea785ca8
In the meantime, you can copy the code into your own app, or you could implement the templ.Component
interface on whatever the Org
type is:
type Org struct {
FieldA string
}
func (o Org) Render(ctx context.Context, w io.Writer) (err error) {
if _, err = io.WriteString(w, "<script type\"application/ld+json\">"); err != nil {
return err
}
if err = json.NewEncoder(w).Encode(j.Data); err != nil {
return err
}
if _, err = io.WriteString(w, "</script>"); err != nil {
return err
}
return nil
}
Thanks @a-h
Hi,
When trying to output JSON data in
<script type="application/ld+json">
the data is escaped:.templ
.go
As a result I'm appending the script type to a marshal func:
Is there a way to do this inine?