Documentation ¶
Overview ¶
caddy-xtemplate is a Caddy module that extends Go's html/template to be capable enough to host an entire server-side application in it.
Index ¶
- type FuncHandlerError
- type FuncsProvider
- type HandlerError
- type TemplateContext
- func (c *TemplateContext) Cookie(name string) string
- func (c *TemplateContext) Exec(query string, params ...any) (result sql.Result, err error)
- func (c *TemplateContext) FileExists(filename string) (bool, error)
- func (c *TemplateContext) Funcs() template.FuncMap
- func (c *TemplateContext) Host() (string, error)
- func (c *TemplateContext) ListFiles(name string) ([]string, error)
- func (c *TemplateContext) QueryRow(query string, params ...any) (map[string]any, error)
- func (c *TemplateContext) QueryRows(query string, params ...any) (rows []map[string]any, err error)
- func (c *TemplateContext) QueryStats() struct{ ... }
- func (c *TemplateContext) QueryVal(query string, params ...any) (any, error)
- func (c *TemplateContext) ReadFile(filename string) (string, error)
- func (c *TemplateContext) RemoteIP() string
- func (c *TemplateContext) StatFile(filename string) (fs.FileInfo, error)
- func (c *TemplateContext) Template(name string, context any) (string, error)
- func (c *TemplateContext) WithVars(vars map[string]any) TemplateContextVars
- type TemplateContextVars
- type Templates
- type WrappedHeader
- type XTemplateModule
- func (XTemplateModule) CaddyModule() caddy.ModuleInfo
- func (m *XTemplateModule) Cleanup() error
- func (m *XTemplateModule) Provision(ctx caddy.Context) error
- func (m *XTemplateModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error
- func (t *XTemplateModule) Validate() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FuncHandlerError ¶
type FuncHandlerError struct {
// contains filtered or unexported fields
}
func (FuncHandlerError) Error ¶
func (fhe FuncHandlerError) Error() string
func (FuncHandlerError) ServeHTTP ¶
func (fhe FuncHandlerError) ServeHTTP(w http.ResponseWriter, r *http.Request)
type FuncsProvider ¶
type HandlerError ¶
type HandlerError interface { Error() string ServeHTTP(w http.ResponseWriter, r *http.Request) }
func NewHandlerError ¶
func NewHandlerError(name string, fn func(w http.ResponseWriter, r *http.Request)) HandlerError
type TemplateContext ¶
type TemplateContext struct { Req *http.Request Params pathmatcher.Params RespHeader WrappedHeader RespStatus func(int) string Config map[string]string // contains filtered or unexported fields }
TemplateContext is the TemplateContext with which HTTP templates are executed.
func (*TemplateContext) Cookie ¶
func (c *TemplateContext) Cookie(name string) string
Cookie gets the value of a cookie with name name.
func (*TemplateContext) FileExists ¶
func (c *TemplateContext) FileExists(filename string) (bool, error)
funcFileExists returns true if filename can be opened successfully.
func (*TemplateContext) Funcs ¶
func (c *TemplateContext) Funcs() template.FuncMap
func (*TemplateContext) Host ¶
func (c *TemplateContext) Host() (string, error)
Host returns the hostname portion of the Host header from the HTTP request.
func (*TemplateContext) ListFiles ¶
func (c *TemplateContext) ListFiles(name string) ([]string, error)
ListFiles reads and returns a slice of names from the given directory relative to the root of c.
func (*TemplateContext) QueryStats ¶
func (c *TemplateContext) QueryStats() struct { Count int TotalDuration time.Duration }
func (*TemplateContext) QueryVal ¶
func (c *TemplateContext) QueryVal(query string, params ...any) (any, error)
func (*TemplateContext) ReadFile ¶
func (c *TemplateContext) ReadFile(filename string) (string, error)
ReadFile returns the contents of a filename relative to the site root. Note that included files are NOT escaped, so you should only include trusted files. If it is not trusted, be sure to use escaping functions in your template.
func (*TemplateContext) RemoteIP ¶
func (c *TemplateContext) RemoteIP() string
RemoteIP gets the IP address of the client making the request.
func (*TemplateContext) StatFile ¶
func (c *TemplateContext) StatFile(filename string) (fs.FileInfo, error)
StatFile returns Stat of a filename
func (*TemplateContext) Template ¶
func (c *TemplateContext) Template(name string, context any) (string, error)
func (*TemplateContext) WithVars ¶
func (c *TemplateContext) WithVars(vars map[string]any) TemplateContextVars
type TemplateContextVars ¶
type TemplateContextVars struct { *TemplateContext Vars map[string]any }
type Templates ¶
type Templates struct { TemplateFS fs.FS ContextFS fs.FS ExtraFuncs []template.FuncMap DB *sql.DB WatchPaths []string Config map[string]string Delims struct{ L, R string } // contains filtered or unexported fields }
type WrappedHeader ¶
WrappedHeader wraps niladic functions so that they can be used in templates. (Template functions must return a value.)
func (WrappedHeader) Add ¶
func (h WrappedHeader) Add(field, val string) string
Add adds a header field value, appending val to existing values for that field. It returns an empty string.
func (WrappedHeader) Del ¶
func (h WrappedHeader) Del(field string) string
Del deletes a header field. It returns an empty string.
func (WrappedHeader) Set ¶
func (h WrappedHeader) Set(field, val string) string
Set sets a header field value, overwriting any other values for that field. It returns an empty string.
type XTemplateModule ¶
type XTemplateModule struct { // The root path from which to load template files within the selected // filesystem (the native filesystem by default). Default is the current // working directory. TemplateRoot string `json:"template_root,omitempty"` // The root path to reference files from within template funcs. The default, // empty string means the local filesystem funcs in templates are disabled. ContextRoot string `json:"context_root,omitempty"` // The template action delimiters. If set, must be precisely two elements: // the opening and closing delimiters. Default: `["{{", "}}"]` Delimiters []string `json:"delimiters,omitempty"` // The database driver and connection string. If set, must be precicely two // elements: the driver name and the connection string. Database struct { Driver string `json:"driver,omitempty"` Connstr string `json:"connstr,omitempty"` } `json:"database,omitempty"` Config map[string]string `json:"config,omitempty"` FuncsModules []string `json:"funcs_modules,omitempty"` // contains filtered or unexported fields }
func (XTemplateModule) CaddyModule ¶
func (XTemplateModule) CaddyModule() caddy.ModuleInfo
CaddyModule returns the Caddy module information.
func (*XTemplateModule) Cleanup ¶
func (m *XTemplateModule) Cleanup() error
func (*XTemplateModule) Provision ¶
func (m *XTemplateModule) Provision(ctx caddy.Context) error
Provision provisions t. Implements caddy.Provisioner.
func (*XTemplateModule) ServeHTTP ¶
func (m *XTemplateModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error
func (*XTemplateModule) Validate ¶
func (t *XTemplateModule) Validate() error
Validate ensures t has a valid configuration. Implements caddy.Validator.