Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Batch ¶
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "net/http/httputil" "strings" "github.com/livebud/slot" ) func main() { view := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { slots := slot.Open(w, r) w.Header().Set("Content-Type", "text/html") script := slots.Slot("script") script.WriteString(`<script src='/index.js'></script>`) slot, err := slots.ReadString() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Fprintf(w, "<h1>%s</h1>", slot) }) frame := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { slots := slot.Open(w, r) slot, err := slots.ReadString() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } slots.Slot("style").WriteString(`<link href='/frame.css'/>`) fmt.Fprintf(w, "<main>\n\t\t\t%s\n\t\t</main>", slot) }) layout := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { slots := slot.Open(w, r) slot, err := slots.ReadString() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } script, err := slots.Slot("script").ReadString() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } style, err := slots.Slot("style").ReadString() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } fmt.Fprintf(w, "<html>\n\t<head>\n\t\t%s\n\t\t%s\n\t</head>\n\t<body>\n\t\t%s\n\t</body>\n</html>", script, style, slot) }) handler := slot.Batch(view, frame, layout) req := httptest.NewRequest("GET", "/", nil) rec := httptest.NewRecorder() handler.ServeHTTP(rec, req) res := rec.Result() response, err := httputil.DumpResponse(res, true) if err != nil { panic(err) } fmt.Println(strings.ReplaceAll(string(response), "\r\n", "\n")) }
Output: HTTP/1.1 200 OK Connection: close Content-Type: text/html <html> <head> <script src='/index.js'></script> <link href='/frame.css'/> </head> <body> <main> <h1></h1> </main> </body> </html>
Types ¶
Click to show internal directories.
Click to hide internal directories.