Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.
/ txtx Public archive

Some stuff to make it easier to server side render HTML templates that can be used client side too.

License

Notifications You must be signed in to change notification settings

crhntr/txtx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

txtx

Archived: Just use HTMX and render the templates on the server.

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>
<head>
	<title>Some Page</title>
	<!-- Load/start wasm_exec.js and compiled wasm binary (for main included below)-->
</head>

<body>
    <div id="main">
        {{template "greeting" . }}
    </div>

    {{.XTemplates}}
</body>

<script type="text/go-template" id="greeting">
    <h1>{{.Message}}</h1>
</script>

</html>
package server

import (
	"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)
    }
}
// +build js wasm

package 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)
}

About

Some stuff to make it easier to server side render HTML templates that can be used client side too.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages