txtx

package module
v0.0.0-...-b94aa83 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2020 License: MIT Imports: 4 Imported by: 0

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Template

type Template struct {
	*template.Template

	XTemplates template.HTML
	// contains filtered or unexported fields
}

func New

func New(tmpl *template.Template, r io.Reader) (*Template, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL