Skip to content

Instantly share code, notes, and snippets.

@yosssi
Created September 23, 2014 13:53
Show Gist options
  • Select an option

  • Save yosssi/74cb7804724acb22b2ff to your computer and use it in GitHub Desktop.

Select an option

Save yosssi/74cb7804724acb22b2ff to your computer and use it in GitHub Desktop.
20140923-ace-02
= doctype html
html
head
title Ace Sample
body
= include inc Input "password" "password" "" "Password" "icon-lock" true true
h3 This is an include file!
h4 {{.Name}}
h4 {{.Type}}
h4 {{.Value}}
h4 {{.Placeholder}}
h4 {{.Icon}}
h4 {{.LoginInput}}
h4 {{.Required}}
package main
import (
"bytes"
"fmt"
"html/template"
"net/http"
"github.com/yosssi/ace"
"github.com/yosssi/gohtml"
)
type Input struct {
Name string
Type string
Value string
Placeholder string
Icon string
LoginInput bool
Required bool
}
func handler(w http.ResponseWriter, r *http.Request) {
tpl, err := ace.Load("base", "inner", &ace.Options{
DynamicReload: true,
FuncMap: template.FuncMap{
"Input": func(name string, t string, value string, placeholder string, icon string, loginInput bool, required bool) Input {
return Input{
Name: name,
Type: t,
Value: value,
Placeholder: placeholder,
Icon: icon,
LoginInput: loginInput,
Required: required,
}
},
},
})
if err != nil {
panic(err)
}
if err := tpl.Execute(w, "test"); err != nil {
bf := new(bytes.Buffer)
tpl.Execute(bf, nil)
fmt.Fprintf(w, "<pre>Error:\n%s\nHTML:\n%s</pre>", err.Error(), template.HTMLEscapeString(gohtml.FormatWithLineNo(bf.String())))
}
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment