Templates are working, just empty
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package view
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"git.aiterp.net/gisle/wrouter/response"
|
||||
)
|
||||
|
||||
var wd, _ = os.Getwd()
|
||||
var rootPath = path.Join(wd, "./view/templates/")
|
||||
var cache = make(map[string]*template.Template)
|
||||
|
||||
// Register registers a template and compiles it for rendering. This should be done
|
||||
// in the beginning since an error will terminate the server
|
||||
func Register(name string, base string) {
|
||||
tmpl, err := template.New(name).ParseFiles(path.Join(rootPath, name+".tmpl"), path.Join(rootPath, base+".tmpl"))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to register %s: %s", name, err)
|
||||
}
|
||||
|
||||
cache[name] = tmpl
|
||||
}
|
||||
|
||||
// Render renders a template with the name it was registered with, and with the
|
||||
// view model. The view model is expected to be the correct model from the viewmodel
|
||||
// package
|
||||
func Render(w http.ResponseWriter, name string, status int, viewModel interface{}) {
|
||||
tmpl, ok := cache[name]
|
||||
if !ok {
|
||||
response.Text(w, 500, "Template not found: "+name)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(status)
|
||||
err := tmpl.ExecuteTemplate(w, "base", viewModel)
|
||||
if err != nil {
|
||||
log.Println("Template error:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// Run runs the template to a simple io.Writer. It's made for testing
|
||||
func Run(w io.Writer, name string, viewModel interface{}) error {
|
||||
tmpl, ok := cache[name]
|
||||
if !ok {
|
||||
return errors.New("template not found")
|
||||
}
|
||||
|
||||
return tmpl.ExecuteTemplate(w, "base", viewModel)
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register("index", "base/default")
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
{{define "layout"}}
|
||||
{{define "base"}}
|
||||
<!DOCTYPE html>
|
||||
<!-- For preview and template creation only -->
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>{ .ViewTitle } - { .SiteTitle }</title>
|
||||
<title>{{ .ViewTitle }}</title>
|
||||
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=no" />
|
||||
@@ -32,27 +32,10 @@
|
||||
</main>
|
||||
|
||||
<nav class="main-menu">
|
||||
|
||||
<h1>{ .ViewTitle }</h1>
|
||||
|
||||
<ul>
|
||||
<li><a><div class="mg-icon">O</div><div class="mg-label">OoC</div></a></li>
|
||||
<li><a><div class="mg-icon">S</div><div class="mg-label">Stories</div></a></li>
|
||||
<li><a><div class="mg-icon">N</div><div class="mg-label">News</div></a></li>
|
||||
<li><a><div class="mg-icon">D</div><div class="mg-label">Documents</div></a></li>
|
||||
<li><a><div class="mg-icon">I</div><div class="mg-label">Items</div></a></li>
|
||||
<li><a><div class="mg-icon">i</div><div class="mg-label">Info</div></a></li>
|
||||
<li><a><div class="mg-icon">B</div><div class="mg-label">Background</div></a></li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li><a><div class="mg-icon">+</div><div class="mg-label">Post</div></a></li>
|
||||
</ul>
|
||||
|
||||
{{ template "menu" }}
|
||||
<ul>
|
||||
<li><a><div class="mg-icon">A</div><div class="mg-label">Login</div></a></li>
|
||||
</ul>
|
||||
|
||||
</nav>
|
||||
</div>
|
||||
</body>
|
||||
@@ -0,0 +1,13 @@
|
||||
{{ define "content" }}
|
||||
<article>
|
||||
<h1>Hello, World</h1>
|
||||
</article>
|
||||
{{ end }}
|
||||
|
||||
{{ define "menu" }}
|
||||
<h1>Aite RP</h1>
|
||||
{{ end }}
|
||||
|
||||
{{ define "head" }}
|
||||
|
||||
{{ end }}
|
||||
Reference in New Issue
Block a user