fazer

package module
v0.12.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2020 License: MIT Imports: 19 Imported by: 1

README

fazer

A library for making templates.

Documentation

Overview

Fazer provides a template caching and rendering solution.

Fazer provides a manifest file for all pages to be rendered.

Index

Constants

View Source
const (
	Flash        = "flash"
	FlashError   = "flasherror"
	FlashSuccess = "flashsuccess"
)

Variables

View Source
var ErrorNoTemplate = errors.New("no template found")
View Source
var ErrorPageNotExists = errors.New("page doesn't exist")
View Source
var ErrorSectionNotExists = errors.New("section doesn't exist")

Functions

func AssetsPath added in v0.10.1

func AssetsPath(path string) func(*Manifest) error

func BaseURL

func BaseURL(base string) func() string

func DateSimple

func DateSimple(in time.Time) string

func HasTrim

func HasTrim(in string) bool

func MRender

func MRender(in string) template.HTML

func Marshal

func Marshal(in interface{}) (template.JS, error)

func PathEscape

func PathEscape(value string) string

func SafeHTML

func SafeHTML(input string) template.HTML

func SetProduction added in v0.10.1

func SetProduction(prod bool) func(*Manifest) error

func Trim

func Trim(in string) string

func Truncate

func Truncate(in string, length int) string

func XUrls

func XUrls(in string) template.HTML

Types

type Fazer

type Fazer struct {
	sessions.Store
	// contains filtered or unexported fields
}

Fazer holds the information required to lookup, cache, and render html templates to be output in a web request.

func NewFazer

func NewFazer(store sessions.Store, tplDir string, config interface{}, prod bool) *Fazer

NewFazer provides the fazer renderer with all required things initialized, including sane template functions. store: Gorilla sessions store. This is used for adding and displaying flash messages. tplDir: This is the directory in the filesystem that contains the templates. This directory can contain sub directories. config: This is any struct that you want to contain config values needed for rendering templates prod: Tells the system if you're running in production. This affects the caching and loading.

func (*Fazer) AddManifest

func (f *Fazer) AddManifest(manifest *Manifest) error

AddManifest registers a manifest with the fazer renderer. This automatically registers the js, css, and img functions with the template.

func (*Fazer) GetFlashes

func (f *Fazer) GetFlashes(w http.ResponseWriter, r *http.Request) ([]interface{}, []interface{})

GetFlashes returns any error or success flashes that are on the session. Usually only to be used internally

func (*Fazer) Partial

func (f *Fazer) Partial(name string, payload interface{}) (template.HTML, error)

Partial allows a partial to be loaded within the template. Use: {{ partial "key" . }}

func (*Fazer) Redirect

func (f *Fazer) Redirect(w http.ResponseWriter, r *http.Request, url string)

Redirect returns a redirect with http.StatusSeeOther

func (*Fazer) RedirectCode

func (f *Fazer) RedirectCode(w http.ResponseWriter, r *http.Request, url string, code int)

RedirectCode returns a redirect with given http status

func (*Fazer) RegisterMerge added in v0.6.5

func (f *Fazer) RegisterMerge(merge Merge)

RegisterMerge adds a merge function to the renderer. This will be called previous to rendering a template.

func (*Fazer) RegisterTemplate

func (f *Fazer) RegisterTemplate(key string, templates ...string) error

RegisterTemplate adds a template list to the renderer.

func (*Fazer) RegisterTemplateFunc

func (f *Fazer) RegisterTemplateFunc(key string, fn interface{})

func (*Fazer) RenderJson

func (f *Fazer) RenderJson(w http.ResponseWriter, r *http.Request, data interface{})

RenderJson is a shortcut method for rendering json to the response

func (*Fazer) RenderTemplate

func (f *Fazer) RenderTemplate(w http.ResponseWriter, r *http.Request, data map[string]interface{}, key string)

RenderTemplate renders a registered html template with an http.StatusOk data: is a map that will be passed to the template rendering. key: this is the key used to register the template group. Will be of form section.page if registered from the manifest, see above. Calls RenderTemplateWithCode internally

func (*Fazer) RenderTemplateWithCode

func (f *Fazer) RenderTemplateWithCode(w http.ResponseWriter, r *http.Request, code int, data map[string]interface{}, key string)

RenderTemplateWithCode renders a registered html template with the given http status code. data: is a map that will be passed to the template rendering. key: this is the key used to register the template group. Will be of form section.page if registered from the manifest, see above. This creates a new map[string]interface{} if the passed in data is nil. The fazer.config interface{} is added under Config The *http.Request is added under Request If a page exists from the manifest with the given key, then it is added under Page FlashError is added for any errors on the flash session FlashSuccess is added for any success on the flash session

func (*Fazer) SetErrorFlash

func (f *Fazer) SetErrorFlash(w http.ResponseWriter, r *http.Request, message string)

SetErrorFlash sets a flash error message if the fazer.store is not nil

func (*Fazer) SetSuccessFlash

func (f *Fazer) SetSuccessFlash(w http.ResponseWriter, r *http.Request, message string)

SetSuccessFlash sets a flash success message if the fazer.store is not nil

type Item

type Item struct {
	Path    string `toml:"path"`
	Min     string `toml:"min"`
	NoMin   bool   `toml:"no-min" json:"no-min"`
	Version int64
}

Item store paths for static assets [js|css|images]

type Manifest

type Manifest struct {
	WorkingDir string
	AssetDir   string
	Production bool

	Sections map[string]Section `toml:"sections"`
	Partials map[string]string  `toml:"partials"`

	Js  map[string]Item `toml:"js"`
	Css map[string]Item `toml:"css"`
	Img map[string]Item `toml:"img"`
}

Manifest is a collection of templates, broken up into sections, javascript, css, and images. Assets are versioned based upon the timestamp of the asset.

func MustDecodeManifestFile

func MustDecodeManifestFile(input string, opts ...func(*Manifest) error) *Manifest

MustDecodeManifest decodes the toml representation of a manifest.

[sections]

	[sections.front]
	base="front/base.tpl"
	[sections.front.pages.index]
	tpls=[":base", "front/index.tpl"]
	title="Page Title"
	description="Page description"
	canonical="http://page.url"
[js]
	[js.vue]
	path="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"
	min="https://cdn.jsdelivr.net/npm/vue"

[css]

[css.bulma]
path=""
min=""

func MustDecodeManifestJson added in v0.8.0

func MustDecodeManifestJson(data string, opts ...func(*Manifest) error) *Manifest

MustDecodeManifestJson decodes the json representation of a manifest.

{
 "sections": {
   "front": {
     "base": "front/base.tpl",
     "pages": {
       "index": {
         "tpls": [
           ":base",
           "front/index.tpl"
         ],
         "title": "Page Title",
         "description": "Page description",
         "canonical": "http://page.url"
       }
		}
	},
	"js": {
   	"vue": {
     	"path": "https://cdn.jsdelivr.net/npm/vue/dist/vue.js",
     	"min": "https://cdn.jsdelivr.net/npm/vue"
   	}
	},
	"css": {
   	"bulma": {
     	"path": "/css/bulma.css",
     	"no-min": true
   	}
	}
}

func (*Manifest) AddCSS added in v0.11.0

func (m *Manifest) AddCSS(key string, css Item)

AddCSS adds an item to the css map

func (*Manifest) AddImg added in v0.11.0

func (m *Manifest) AddImg(key string, img Item)

AddImg adds an item to the image map

func (*Manifest) AddJS added in v0.11.0

func (m *Manifest) AddJS(key string, js Item)

AddJS adds an item to the javascript map

func (*Manifest) AddPage added in v0.11.0

func (m *Manifest) AddPage(sectionName, pageName string, page *Page) error

AddPage to a section

func (*Manifest) AddPartial added in v0.11.1

func (m *Manifest) AddPartial(key, path string)

Add a partial to the manifest

func (*Manifest) CreateSection added in v0.11.0

func (m *Manifest) CreateSection(name, baseTpl string)

CreateSection adds a section to this manifest

func (*Manifest) Finalize added in v0.11.0

func (m *Manifest) Finalize(opts ...func(*Manifest) error)

Finalize finishes setting up the manifest. The woringdir and assetdir is set. The sections setup the templates for the pages. Also the asset items are setup to use minified versions and appends the timestamp for caching purposes.

func (*Manifest) GetCssPath

func (m *Manifest) GetCssPath(key string) string

GetCssPath is intended to be registered with the fazer template rendering. This will return the path for the keyed item. This reflects the environment prod or not, returning a minified or regular path.

func (*Manifest) GetImgPath

func (m *Manifest) GetImgPath(key string) string

GetImgPath is intended to be registered with the fazer template rendering. This will return the path for the keyed item. This reflects the environment prod or not, returning a minified or regular path.

func (*Manifest) GetJsPath

func (m *Manifest) GetJsPath(key string) string

GetJsPath is intended to be registered with the fazer template rendering. This will return the path for the keyed item. This reflects the environment prod or not, returning a minified or regular path.

func (*Manifest) IteratePages added in v0.6.3

func (m *Manifest) IteratePages() chan *Page

IteratePages runs over every page, Fazer uses this to register templates.

func (*Manifest) LookupPage added in v0.6.3

func (m *Manifest) LookupPage(path string) (*Page, bool)

LookupPage gets the page from a section.page key. This key is added in the fazer renderer, based upon the IteratePages path creation

func (*Manifest) String

func (m *Manifest) String() string

type Merge added in v0.6.5

type Merge func(r *http.Request, data map[string]interface{})

Merge is a function that allows the user to register a method that will be called previous to rendering. This is useful if you want to inspect the path and add data to the rendering context.

type Message added in v0.8.0

type Message struct {
	Plain []string `toml:"plain"`
	Html  []string `toml:"html"`
	// contains filtered or unexported fields
}

type MessageType added in v0.8.0

type MessageType int
const (
	Plain MessageType = iota
	Html
)

type Messages added in v0.8.0

type Messages map[string]Message

type Messenger added in v0.8.0

type Messenger struct {
	// contains filtered or unexported fields
}

func MustDecodeMessageManifest added in v0.8.0

func MustDecodeMessageManifest(data, tplDir string, production bool) *Messenger

func MustDecodeMessageManifestFile added in v0.8.0

func MustDecodeMessageManifestFile(path, tplDir string, production bool) *Messenger

func (*Messenger) Finalize added in v0.12.3

func (mgr *Messenger) Finalize()

func (*Messenger) RegisterTemplateFunc added in v0.8.0

func (m *Messenger) RegisterTemplateFunc(key string, fn interface{})

func (*Messenger) RenderTemplate added in v0.8.0

func (m *Messenger) RenderTemplate(key string, data map[string]interface{}, w io.Writer, mt MessageType) error

type Page

type Page struct {
	ID string `toml:"id"`

	Title              string   `toml:"title"`
	Description        string   `toml:"description"`
	Tpls               []string `toml:"tpls"`
	Canonical          string   `toml:"canonical"`
	OgURL              string   `toml:"ogurl"`
	OgTitle            string   `toml:"ogtitle"`
	OgDescription      string   `toml:"ogdescription"`
	OgImage            string   `toml:"ogimage"`
	OgImageType        string   `toml:"ogimagetype"`
	OgImageWidth       string   `toml:"ogimagewidth"`
	OgImageHeight      string   `toml:"ogimageheight"`
	FbAppID            string   `toml:"fbappid"`
	OgType             string   `toml:"ogtype"`
	OgLocale           string   `toml:"oglocale"`
	TwitterCard        string   `toml:"twittercard"`
	TwitterSite        string   `toml:"twittersite"`
	TwitterTitle       string   `toml:"twittertitle"`
	TwitterDescription string   `toml:"twitterdescription"`
	TwitterCreator     string   `toml:"twittercreator"`
	TwitterImage       string   `toml:"twitterimage"`
	// contains filtered or unexported fields
}

Page provides a representation of a renderedable or an output html file. Title: this is the html meta title Description: this is the html meta description Tpls: this is an array of templates required to render this page. Canonical: this is the html canonical meta

func (Page) GetDescription

func (p Page) GetDescription(def string) string

GetDescription returns page description, intended to be called from the template with a default value

func (Page) GetFbAppId added in v0.9.1

func (p Page) GetFbAppId(def string) string

GetFbAppId returns the fb app id or the default value passed in

func (Page) GetOgDescription added in v0.9.1

func (p Page) GetOgDescription(def string) string

GetOgDescription returns the open graph description, the regular description, or the default value passed in

func (Page) GetOgImage added in v0.9.1

func (p Page) GetOgImage(def string) string

GetOgImage returns the open graph image or the default value passed in

func (Page) GetOgLocale added in v0.9.1

func (p Page) GetOgLocale() string

GetOgLocale returns the open graph locale or the default locale

func (Page) GetOgTitle added in v0.9.1

func (p Page) GetOgTitle(def string) string

GetOgTitle returns the open graph title for the page, or the regular title, or the default value passed in

func (Page) GetOgType added in v0.9.1

func (p Page) GetOgType() string

GetOgType returns the open graph type or the default website

func (Page) GetOgUrl added in v0.9.1

func (p Page) GetOgUrl() string

GetOgUrl returns the open graph url or the canonical url

func (Page) GetTitle

func (p Page) GetTitle(def string) string

GetTitle returns the page title, intended to be called from the template with a default value

func (Page) GetTwitterCard added in v0.10.1

func (p Page) GetTwitterCard(def string) string

GetTwitterCard returns the twitter card summary

func (Page) GetTwitterCreator added in v0.10.1

func (p Page) GetTwitterCreator(def string) string

GetTwitterCreator returns the twitter creator or default

func (Page) GetTwitterDescription added in v0.10.1

func (p Page) GetTwitterDescription(def string) string

GetTwitterDescription returns the twitter description or the page description or the default

func (Page) GetTwitterImage added in v0.10.1

func (p Page) GetTwitterImage(def string) string

GetTwitterImage returns the twitter image or default

func (Page) GetTwitterSite added in v0.10.1

func (p Page) GetTwitterSite(def string) string

GetTwitterSite returns the twitter site or default. i.e. @KendellFab

func (Page) GetTwitterTitle added in v0.10.1

func (p Page) GetTwitterTitle(def string) string

GetTwitterTitle returns the twitter title or the page title or the default

type Section

type Section struct {
	Base  string           `toml:"base"`
	Pages map[string]*Page `toml:"pages"`
}

Section provides a logical grouping of templates, i.e. front and admin. The base property, is encoded in the manifest file as :base. This way you can specify a base template for the section, without constantly duplicating the string. Pages is a map of all of the pages registered in the section.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL