Skip to content
This repository was archived by the owner on Mar 18, 2019. It is now read-only.

apisite/mulate

Repository files navigation

mulate

golang package for multiple layouts & templates processing by single handler

GoCard GitHub Release GitHub code size in bytes GitHub license

  • Project status: MVP is ready
  • Future plans: tests & docs

This package offers 2-step template processing, where page content template called first, so it can

  1. change page layout (among them previous markup)
  2. abort processing and return error page (this will go to way 1)
  3. abort processing and return redirect

If page content template returns HTML, at step 2, layout template will be called for result page markup build.

Request processing flow

Request processing flow

Template structure

According to sample, site templates tree might looks like:

tmpl
├── inc
│   ├── footer.tmpl
│   ├── head.tmpl
│   └── menu.tmpl
├── layout
│   ├── default.tmpl
│   ├── error.tmpl
│   └── wide.tmpl
└── page
    ├── admin
    │   └── index.tmpl
    ├── err.tmpl
    ├── index.tmpl
    ├── page.tmpl
    └── redir.tmpl

Usage

import "github.com/apisite/mulate"

func main() {

    mlt := mulate.New(cfg.Template)
    mlt.DisableCache(true)

    allFuncs := make(template.FuncMap, 0)

    err = mlt.LoadTemplates(allFuncs)

    for _, uri := range mlt.Pages() {
        log.Debugf("Registering uri: %s", uri)
        http.HandleFunc("/"+uri, handleHTML(mlt, uri, log))
    }
}  

See also

Template methods

Get http.Request data

{{ .Request.Host }}{{ .Request.URL.String }}

Get query params

{{ $param := .Request.URL.Query.Get "param" -}}

Set page title

{{ .SetTitle "admin:index" -}}

Choose layout

{{ .SetLayout "wide" -}}

Stop template processing and raise error

{{ .Raise 403 "Test error" "Error description" true }}

Stop template processing and return redirect

{{ .RedirectFound "/page" }}

Custom methods

in code

reqFuncs["data"] = func() interface{} {
    return data
}
p, err := mlt.RenderPage(uri, reqFuncs, r)

in templates

{{range data.Todos -}}
    <li>{{- .Title }}
{{end -}}

TODO

  • docs, part 1
  • tests, part 1
  • google and ask reddit for analogs
  • tests, part 2
  • docs, part 2
  • release

Library name

mulate means multiple layouts & templates.

License

The MIT License (MIT), see LICENSE.

Copyright (c) 2018 Aleksei Kovrizhkin lekovr+apisite@gmail.com

About

golang library for multiple layouts & templates

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages