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 ¶
- func HTML(ctx context.Context, body string, keys map[string]Templateable, ...) (string, error)
- func HTMLSafe(ctx context.Context, body string, keys map[string]Templateable, ...) (string, error)
- func TestSafe(ctx context.Context, body string, keys map[string]Templateable, ...) (string, error)
- func Text(ctx context.Context, body string, keys map[string]Templateable, ...) (string, error)
- type Keys
- type Templateable
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 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.