2017-10-31 16:42:15 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
2017-12-01 16:07:21 +00:00
|
|
|
"io"
|
2017-10-31 16:42:15 +00:00
|
|
|
"net/http"
|
2017-11-08 03:48:22 +00:00
|
|
|
"strings"
|
2017-11-27 03:44:36 +00:00
|
|
|
"time"
|
2017-11-07 17:01:02 +00:00
|
|
|
|
|
|
|
"github.com/kirsle/blog/core/forms"
|
2017-11-08 03:48:22 +00:00
|
|
|
"github.com/kirsle/blog/core/models/settings"
|
2017-11-07 17:53:02 +00:00
|
|
|
"github.com/kirsle/blog/core/models/users"
|
2017-10-31 16:42:15 +00:00
|
|
|
)
|
|
|
|
|
2017-11-07 17:01:02 +00:00
|
|
|
// Vars is an interface to implement by the templates to pass their own custom
|
|
|
|
// variables in. It auto-loads global template variables (site name, etc.)
|
|
|
|
// when the template is rendered.
|
|
|
|
type Vars struct {
|
2017-12-01 16:07:21 +00:00
|
|
|
// Global, "constant" template variables.
|
2017-12-23 21:22:51 +00:00
|
|
|
SetupNeeded bool
|
|
|
|
Title string
|
|
|
|
Path string
|
|
|
|
LoggedIn bool
|
|
|
|
CurrentUser *users.User
|
|
|
|
CSRF string
|
2017-12-23 22:48:47 +00:00
|
|
|
Editable bool // page is editable
|
2017-12-23 21:22:51 +00:00
|
|
|
Request *http.Request
|
|
|
|
RequestTime time.Time
|
|
|
|
RequestDuration time.Duration
|
2017-10-31 16:42:15 +00:00
|
|
|
|
2017-12-01 16:07:21 +00:00
|
|
|
// Configuration variables
|
|
|
|
NoLayout bool // don't wrap in .layout.html, just render the template
|
|
|
|
|
2017-11-07 17:01:02 +00:00
|
|
|
// Common template variables.
|
|
|
|
Message string
|
2017-11-20 05:49:19 +00:00
|
|
|
Flashes []string
|
2017-11-07 17:01:02 +00:00
|
|
|
Error error
|
2017-11-15 14:55:15 +00:00
|
|
|
Data map[interface{}]interface{}
|
2017-11-07 17:01:02 +00:00
|
|
|
Form forms.Form
|
|
|
|
}
|
|
|
|
|
2017-11-15 14:55:15 +00:00
|
|
|
// NewVars initializes a Vars struct with the custom Data map initialized.
|
|
|
|
// You may pass in an initial value for this map if you want.
|
|
|
|
func NewVars(data ...map[interface{}]interface{}) *Vars {
|
|
|
|
var value map[interface{}]interface{}
|
|
|
|
if len(data) > 0 {
|
|
|
|
value = data[0]
|
|
|
|
} else {
|
|
|
|
value = make(map[interface{}]interface{})
|
|
|
|
}
|
|
|
|
return &Vars{
|
|
|
|
Data: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:01:02 +00:00
|
|
|
// LoadDefaults combines template variables with default, globally available vars.
|
2017-12-01 16:07:21 +00:00
|
|
|
func (v *Vars) LoadDefaults(b *Blog, r *http.Request) {
|
2017-11-08 03:48:22 +00:00
|
|
|
// Get the site settings.
|
|
|
|
s, err := settings.Load()
|
|
|
|
if err != nil {
|
|
|
|
s = settings.Defaults()
|
|
|
|
}
|
|
|
|
|
2017-11-15 14:55:15 +00:00
|
|
|
if s.Initialized == false && !strings.HasPrefix(r.URL.Path, "/initial-setup") {
|
2017-11-08 03:48:22 +00:00
|
|
|
v.SetupNeeded = true
|
|
|
|
}
|
2017-11-24 19:56:32 +00:00
|
|
|
v.Request = r
|
2017-12-23 21:22:51 +00:00
|
|
|
v.RequestTime = r.Context().Value(requestTimeKey).(time.Time)
|
2017-11-08 03:48:22 +00:00
|
|
|
v.Title = s.Site.Title
|
|
|
|
v.Path = r.URL.Path
|
2017-11-07 17:53:02 +00:00
|
|
|
|
2017-11-24 20:53:13 +00:00
|
|
|
user, err := b.CurrentUser(r)
|
|
|
|
v.CurrentUser = user
|
|
|
|
v.LoggedIn = err == nil
|
2017-10-31 16:42:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 16:07:21 +00:00
|
|
|
// // TemplateVars is an interface that describes the template variable struct.
|
|
|
|
// type TemplateVars interface {
|
|
|
|
// LoadDefaults(*Blog, *http.Request)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// RenderPartialTemplate handles rendering a Go template to a writer, without
|
|
|
|
// doing anything extra to the vars or dealing with net/http. This is ideal for
|
|
|
|
// rendering partials, such as comment partials.
|
|
|
|
//
|
|
|
|
// This will wrap the template in `.layout.gohtml` by default. To render just
|
|
|
|
// a bare template on its own, i.e. for partial templates, create a Vars struct
|
|
|
|
// with `Vars{NoIndex: true}`
|
|
|
|
func (b *Blog) RenderPartialTemplate(w io.Writer, path string, v interface{}, withLayout bool, functions map[string]interface{}) error {
|
|
|
|
var (
|
|
|
|
layout Filepath
|
|
|
|
templateName string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Find the file path to the template.
|
2017-10-31 16:42:15 +00:00
|
|
|
filepath, err := b.ResolvePath(path)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("RenderTemplate(%s): file not found", path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-01 16:07:21 +00:00
|
|
|
// Get the layout template.
|
|
|
|
if withLayout {
|
|
|
|
templateName = "layout"
|
|
|
|
layout, err = b.ResolvePath(".layout")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("RenderTemplate(%s): layout template not found", path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
templateName = filepath.Basename
|
|
|
|
}
|
|
|
|
|
2017-11-26 23:53:10 +00:00
|
|
|
// The comment entry partial.
|
|
|
|
commentEntry, err := b.ResolvePath("comments/entry.partial")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("RenderTemplate(%s): comments/entry.partial not found")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-01 16:07:21 +00:00
|
|
|
// Template functions.
|
|
|
|
funcmap := template.FuncMap{
|
2017-11-20 05:49:19 +00:00
|
|
|
"StringsJoin": strings.Join,
|
2017-11-27 03:44:36 +00:00
|
|
|
"Now": time.Now,
|
2017-12-01 16:07:21 +00:00
|
|
|
"RenderIndex": b.RenderIndex,
|
2017-11-24 19:56:32 +00:00
|
|
|
"RenderPost": b.RenderPost,
|
2017-12-23 02:34:58 +00:00
|
|
|
"RenderTags": b.RenderTags,
|
2017-12-23 22:48:47 +00:00
|
|
|
"TemplateName": func() string {
|
|
|
|
return filepath.URI
|
|
|
|
},
|
2017-12-01 16:07:21 +00:00
|
|
|
}
|
|
|
|
if functions != nil {
|
|
|
|
for name, fn := range functions {
|
|
|
|
funcmap[name] = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Useful template functions.
|
|
|
|
t := template.New(filepath.Absolute).Funcs(funcmap)
|
2017-11-20 05:49:19 +00:00
|
|
|
|
2017-10-31 16:42:15 +00:00
|
|
|
// Parse the template files. The layout comes first because it's the wrapper
|
|
|
|
// and allows the filepath template to set the page title.
|
2017-12-01 16:07:21 +00:00
|
|
|
var templates []string
|
|
|
|
if withLayout {
|
|
|
|
templates = append(templates, layout.Absolute)
|
|
|
|
}
|
|
|
|
t, err = t.ParseFiles(append(templates, commentEntry.Absolute, filepath.Absolute)...)
|
2017-10-31 16:42:15 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-01 16:07:21 +00:00
|
|
|
err = t.ExecuteTemplate(w, templateName, v)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Template parsing error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenderTemplate responds with an HTML template.
|
|
|
|
//
|
|
|
|
// The vars will be massaged a bit to load the global defaults (such as the
|
|
|
|
// website title and user login status), the user's session may be updated with
|
|
|
|
// new CSRF token, and other such things. If you just want to render a template
|
|
|
|
// without all that nonsense, use RenderPartialTemplate.
|
|
|
|
func (b *Blog) RenderTemplate(w http.ResponseWriter, r *http.Request, path string, vars *Vars) error {
|
2017-10-31 16:42:15 +00:00
|
|
|
// Inject globally available variables.
|
2017-11-07 17:01:02 +00:00
|
|
|
if vars == nil {
|
|
|
|
vars = &Vars{}
|
|
|
|
}
|
2017-12-01 16:07:21 +00:00
|
|
|
vars.LoadDefaults(b, r)
|
2017-10-31 16:42:15 +00:00
|
|
|
|
2017-12-01 16:07:21 +00:00
|
|
|
// Add any flashed messages from the endpoint controllers.
|
|
|
|
session := b.Session(r)
|
|
|
|
if flashes := session.Flashes(); len(flashes) > 0 {
|
|
|
|
for _, flash := range flashes {
|
|
|
|
_ = flash
|
|
|
|
vars.Flashes = append(vars.Flashes, flash.(string))
|
|
|
|
}
|
|
|
|
session.Save(r, w)
|
2017-10-31 16:42:15 +00:00
|
|
|
}
|
|
|
|
|
2017-12-23 21:22:51 +00:00
|
|
|
vars.RequestDuration = time.Now().Sub(vars.RequestTime)
|
2017-12-01 16:07:21 +00:00
|
|
|
vars.CSRF = b.GenerateCSRFToken(w, r, session)
|
2017-12-23 22:48:47 +00:00
|
|
|
vars.Editable = !strings.HasPrefix(path, "admin/")
|
2017-12-01 16:07:21 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/html; encoding=UTF-8")
|
|
|
|
b.RenderPartialTemplate(w, path, vars, true, template.FuncMap{
|
|
|
|
"RenderComments": func(subject string, ids ...string) template.HTML {
|
|
|
|
session := b.Session(r)
|
|
|
|
csrf := b.GenerateCSRFToken(w, r, session)
|
|
|
|
return b.RenderComments(session, csrf, r.URL.Path, subject, ids...)
|
|
|
|
},
|
|
|
|
})
|
2017-10-31 16:42:15 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|