easytpl

package module
v0.0.0-...-7a667fb Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2020 License: MIT Imports: 9 Imported by: 0

README

Build Status codecov GoDoc

A very simple template system, intended for simple customer-facing templates.

easytpl transforms to Go's template system as follows:

{%var%}                          -> {{Var}}
{%var.val%}                      -> {{Var.Val}}
{%var.val,fallback=some string%} -> {{if .Var.Val}}{{.Var.Val}}{{else}}some string{{end}}
{{var.val}}                      -> {{ "{{var.val}}" }}

Template variables can also be escaped so that they are not translated into Go's template system, like so:

\{%var%} -> {%var%}

Function calls

{%@user.HasPermission "feature-x"%} -> {{call .user.HasPermission "feature-x"}}

That's all :-) It doesn't support if, range, or anything else.

Documentation

Overview

Package easytpl is a very simple template system.

easytpl transforms to Go's template system as follows:

{%var%}                          -> {{Var}}
{%var.val%}                      -> {{Var.Val}}
{%var.val,fallback=some string%} -> {{if .Var.Val}}{{.Var.Val}}{{else}}some string{{end}}
{{var.val}}                      -> {{ "{{var.val}}" }}

That's all :-) It doesn't support if, range, or anything else.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTML

func HTML(ctx context.Context, body string, keys map[string]Templateable, parentKeys Keys) (string, error)

HTML works exactly like Text() but uses the html/template package so it will escape the variables values.

func HTMLSafe

func HTMLSafe(ctx context.Context, body string, keys map[string]Templateable, parentKeys Keys) (string, error)

HTMLSafe works exactly like HTML() but will fall back to Text() if HTML() fails. This is to allow people to use broken HTML. We can't fix the world, unfortunately.

HTML() errors will be logged. Errors from Text() will be returned.

func TestSafe

func TestSafe(ctx context.Context, body string, keys map[string]Templateable, parentKeys Keys) (string, error)

TestSafe will test the given data strictly against the HTMLSafe standard to find variable errors in particular.

func Text

func Text(ctx context.Context, body string, keys map[string]Templateable, parentKeys Keys) (string, error)

Text attempts to parse the template with text/template. The templatables in keys will be used as template parameters; for example with:

map[string]Templateable{
    "Inbox": inbox,
}

You get {%inbox.ID%} (or any other keys that the inbox type has).

The parentKeys are always passed to every Templateable; it's useful to pass some global state around (e.g. context, session, etc.)

If processing fails the original template and an error are returned.

Types

type Keys

type Keys map[string]interface{}

Keys are parameters passed to the template.

type Templateable

type Templateable interface {
	// TemplateKeys returns a zero or more keys to be used as template
	// parameters; e.g.
	//
	//   return easytpl.Params{
	//   	"ID": obj.ID,
	//   }
	//
	// The parentKeys are what you passed in with Text()/HTML()/HTMLSafe().
	TemplateKeys(ctx context.Context, parentKeys Keys) (params Keys)

	// TemplateCallbacks allows getting a template key dynamically only when
	// it's used. The advantage of this is performance (some variables are
	// expensive but not used a lot).
	//
	// If setIt is false, we won't set anything for this key.
	TemplateCallbacks(ctx context.Context, key string, parentKeys Keys) (value string, setIt bool)
}

Templateable allows template substitution.

Jump to

Keyboard shortcuts

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