diff --git a/README.md b/README.md index b888fd54d..62839a00b 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,12 @@ go tool covdata textfmt -i=./coverage/fmt,./coverage/generate,./coverage/version go tool cover -func coverage.out | grep total ``` +### test-cover-watch + +```sh +gotestsum --watch -- -coverprofile=coverage.out +``` + ### benchmark Run benchmarks. diff --git a/docs/docs/03-syntax-and-usage/12-script-templates.md b/docs/docs/03-syntax-and-usage/12-script-templates.md index a3495f4ac..d197a66f6 100644 --- a/docs/docs/03-syntax-and-usage/12-script-templates.md +++ b/docs/docs/03-syntax-and-usage/12-script-templates.md @@ -2,7 +2,7 @@ ## Scripts -Use standard ` +``` + +The data in the script tag can then be accessed from client-side JavaScript. + +```javascript +const data = JSON.parse(document.getElementById('id').textContent); +``` + +## Working with NPM projects + +https://github.com/a-h/templ/tree/main/examples/typescript contains a TypeScript example that uses `esbuild` to transpile TypeScript into plain JavaScript, along with any required `npm` modules. + +After transpilation and bundling, the output JavaScript code can be used in a web page by including a ` + +} +``` + +You will need to configure your Go web server to serve the static content. + +```go title="main.go" +func main() { + mux := http.NewServeMux() + // Serve the JS bundle. + mux.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))) + + // Serve components. + data := map[string]any{"msg": "Hello, World!"} + h := templ.Handler(components.Page(data)) + mux.Handle("/", h) + + fmt.Println("Listening on http://localhost:8080") + http.ListenAndServe("localhost:8080", mux) +} +``` + ## Script templates +:::warning +Script templates are a legacy feature and are not recommended for new projects. Use standard ``, string(dataJSON)); err != nil { - return err - } - return nil - }) -} - templ Page(attributeData Data, scriptData Data) { @@ -46,8 +12,8 @@ templ Page(attributeData Data, scriptData Data) {
- - @JSONScript("scriptData", scriptData) + + @templ.JSONScript("scriptData", scriptData)