-
-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add JSONString and JSONScript functions, update docs, refer to …
…templ script as legacy in docs (#745) Co-authored-by: Joe Davidson <[email protected]>
- Loading branch information
Showing
12 changed files
with
314 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.2.698 | ||
0.2.700 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package templ | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
) | ||
|
||
var _ Component = JSONScriptElement{} | ||
|
||
// JSONScript renders a JSON object inside a script element. | ||
// e.g. <script type="application/json">{"foo":"bar"}</script> | ||
func JSONScript(id string, data any) JSONScriptElement { | ||
return JSONScriptElement{ | ||
ID: id, | ||
Data: data, | ||
Nonce: GetNonce, | ||
} | ||
} | ||
|
||
func (j JSONScriptElement) WithNonceFromString(nonce string) JSONScriptElement { | ||
j.Nonce = func(context.Context) string { | ||
return nonce | ||
} | ||
return j | ||
} | ||
|
||
func (j JSONScriptElement) WithNonceFrom(f func(context.Context) string) JSONScriptElement { | ||
j.Nonce = f | ||
return j | ||
} | ||
|
||
type JSONScriptElement struct { | ||
// ID of the element in the DOM. | ||
ID string | ||
// Data that will be encoded as JSON. | ||
Data any | ||
// Nonce is a function that returns a CSP nonce. | ||
// Defaults to CSPNonceFromContext. | ||
// See https://content-security-policy.com/nonce for more information. | ||
Nonce func(ctx context.Context) string | ||
} | ||
|
||
func (j JSONScriptElement) Render(ctx context.Context, w io.Writer) (err error) { | ||
var nonceAttr string | ||
if nonce := j.Nonce(ctx); nonce != "" { | ||
nonceAttr = fmt.Sprintf(" nonce=\"%s\"", EscapeString(nonce)) | ||
} | ||
if _, err = fmt.Fprintf(w, "<script id=\"%s\" type=\"application/json\"%s>", EscapeString(j.ID), nonceAttr); 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 | ||
} |
Oops, something went wrong.