Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/crhntr/txtx
Some stuff to make it easier to server side render HTML templates that can be used client side too.
https://github.com/crhntr/txtx
Last synced: about 1 month ago
JSON representation
Some stuff to make it easier to server side render HTML templates that can be used client side too.
- Host: GitHub
- URL: https://github.com/crhntr/txtx
- Owner: crhntr
- License: mit
- Created: 2020-04-29T04:41:33.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-02-25T00:32:14.000Z (almost 2 years ago)
- Last Synced: 2024-06-20T16:51:18.972Z (6 months ago)
- Language: Go
- Size: 10.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# txtx
This package parses a go html template.
Finds "script" elements with the tag "text/go-template".
Provides a string of type template.HTML so that the template can be used again client side.
Useful for GOOS=js GOARCH=wasm + Go templates.## (wip) Example
Please note github.com/crhntr/dom is unstable. the following may not compile.
It is included to convey why you might want to copy some code from this package.```html
Some Page
{{template "greeting" . }}
{{.XTemplates}}
<h1>{{.Message}}</h1>
```
```go
package serverimport (
"html/template"
"net/http"
"os""github.com/crhntr/txtx"
)func SomePage() http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
f, _ := os.Open("pages/some-page/index.html")
tmp, _ := txtx.New(template.New(""), f)
var data = struct{
XTemplates template.HTML
Message string
} {
XTemplates: tmp.XTemplates,
Message: "Hello, world!",
}
res.WriteHeader(http.StatusOK)
_ = tmp.ExecuteTemplate(res, "index.html", data)
}
}
``````go
// +build js wasmpackage main
import (
"html/template"
"net/http"
"os"
"time""github.com/crhntr/dom"
)func main() {
tmp, _ := dom.LoadTemplates((*template.Template)(nil), "")
time.Sleep(time.Second * 5)
mainEl = dom.GetElementByID("main")
greeting, _ := dom.NewElementFromTemplate(tmp, "greeting", struct{
Message string
} { "Hola, mundo!" })
mainEl.SetInnerHTML("")
mainEl.Call("appendChild", greeting)
}
```