xtemplate

package module
v0.0.0-...-5408201 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2022 License: MIT Imports: 18 Imported by: 0

README

GoDoc

xtemplate

a wrapper around text/template inspired by:

Overview

xtemplate wraps text/template to provide:

  • file based inheritance
  • optional caching
  • syntax sugar on function/method calls (eg fn(arg1, arg2) instead of fn arg1 arg2)
  • syntax sugar on template calls
  • VueJs like components
<!-- master.html -->
<html>
<body>
<h1>
  Hello from:
  {{block "source" }} master {{end}}
</h1>
</body>
</html>
<!-- extras.html -->
{{ define "footer" }} footer {{end}}
<!-- child.html -->
{{ extends "master.html" }}
{{ include "extra.html" }}

{{ define "source" }}
child
{{ template footer }}
{{end}}

rendering child.html will output

Hello from child footer

Syntax sugar

C like function calls

{{ login("name", "password") }}

instead of
{{ login "name" "password" }}

make template calls look like calling functions

use defined template blocks (imported using the include directive)

{{define "hello"}}
Hello {{ .name }}, age: {{.age}}
{{end}}

{{define "button"}}
a {{ upper("button") }} {{ . }}
{{end}}

call the blocks as follows

{{ macro button("submit") }}

or

{{ template button("submit") }}

or

{{ macro hello("name"::"ayo", "age"::18) }}

Syntax sugar: custom tags


<tag type="field" label="name">
  <input>
</tag>

gets translated to a function call to a user defined tag template function
which then generates the output
tag (type, attributes, content)

Components

The component feature transforms a template with the same name as the value of the type attribute must exist in the "_components" subfolder

Slots

A component can define a slot which can be overridden when it is called


<component type="card">
  <slot name="title">A title</slot>
</component>

if a component defines a default slot the content of the component tag will be placed within the components default slot


<component type="card">
  this will go in cards default slot if card defines one
</component>
Component Templates

Component templates are valid text/template file with special semantics


<div class="box">
  {{block "#slot--default" .}}
  default content
  {{end}}
</div>

<component type="box">
  lorem ipsum
</component>
Context and Attributes

The context passed into a component is of type map[string]interface{} the parent context is stored in ' ctx' (.ctx.Name) and component attributes such id|class|style are stored in 'props' (.props.style)


<component type="box" class="red">
  lorem ipsum
</component>

<div class="box {{$.props.class}}">
  {{block "#slot--default" .}}
  default content
  {{end}}
</div>

Documentation

Index

Constants

View Source
const (
	OpeningTag tagType = iota
	ClosingTag
)
View Source
const (
	OpeningAction actionType = iota
	ClosingAction
	SingleAction
)

Variables

This section is empty.

Functions

func IfEmpty

func IfEmpty(val interface{}, values ...interface{}) interface{}

IfEmpty if val is empty return the first non-empty item in values

func IsEmpty

func IsEmpty(val interface{}) bool

IsEmpty returns true if val is nil empty (nil or a zero value for its type)

func IsMap

func IsMap(val interface{}) bool

IsMap ...

func IsSlice

func IsSlice(val interface{}) bool

IsSlice ...

func MixAsset

func MixAsset(publicPath string) func(val string) string

MixAsset reads a laravel-mix mix-manifest.json file and returns the hashed filename. assumes that the file will be in ./static

func NoCache

func NoCache(file string) string

func StrListIncludes

func StrListIncludes(str string, list []string) bool

Types

type Action

type Action struct {
	Name     string
	Attr     string
	ID       string
	StartPos int
	EndPos   int
	Body     string
	Type     actionType
}

type Attr

type Attr struct {
	Key   string
	Value string
	Src   string
}

type Config

type Config struct {
	RootFolder       string
	PartialsFolder   string
	ComponentsFolder string
	Ext              string
	Funcs            template.FuncMap
}

type Document

type Document []byte

func (*Document) Append

func (d *Document) Append(docs ...[]byte)

func (*Document) AppendString

func (d *Document) AppendString(docs ...string)

func (*Document) Cut

func (d *Document) Cut(start, end int)

func (*Document) CutAndInsert

func (d *Document) CutAndInsert(start, end int, data []byte)

func (*Document) Replace

func (d *Document) Replace(old, new []byte, n int)

func (*Document) String

func (d *Document) String() string

type IncludeFile

type IncludeFile string

func (IncludeFile) FromTemplateAction

func (i IncludeFile) FromTemplateAction() bool

func (IncludeFile) Name

func (i IncludeFile) Name() string

func (*IncludeFile) SetTemplateActionFlag

func (i *IncludeFile) SetTemplateActionFlag()

func (IncludeFile) String

func (i IncludeFile) String() string

type Tag

type Tag struct {
	Element  string
	ID       string
	Name     string
	StartPos int
	EndPos   int
	Type     tagType
	Body     string
	Attr     TagAttr
}

func (Tag) Equal

func (t Tag) Equal(tag *Tag) bool

type TagAttr

type TagAttr map[string]*Attr

func (*TagAttr) Add

func (a *TagAttr) Add(key, val, src string)

func (*TagAttr) Merge

func (a *TagAttr) Merge(attrs TagAttr)

type XTemplate

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

XTemplate ...

func New

func New(cfg Config) *XTemplate

New create new instance of XTemplate

func (*XTemplate) AddFunc

func (s *XTemplate) AddFunc(name string, fn interface{}) *XTemplate

AddFunc add a function the template's function map. must be called before templates are parsed

func (*XTemplate) Delims

func (s *XTemplate) Delims(left, right string) *XTemplate

Delims sets the template delimiters to the specified strings, must be called before templates are parsed

func (*XTemplate) Funcs

func (s *XTemplate) Funcs(funcMap template.FuncMap) *XTemplate

Funcs adds the elements of the argument map to the template's function map. must be called before templates are parsed

func (*XTemplate) ListFuncs

func (s *XTemplate) ListFuncs()

func (*XTemplate) Lookup

func (s *XTemplate) Lookup(name string) *template.Template

Lookup returns the template with the given name in the cache

func (*XTemplate) ParseFile

func (s *XTemplate) ParseFile(name string) error

ParseFile ...

func (*XTemplate) Render

func (s *XTemplate) Render(wr io.Writer, name string, data interface{}, ignoreCache bool) error

Render parses a template then caches it. Will use cached version unless ignoreCache == true if the template isnt found in the cache Render will attempt to locate it and parse

func (*XTemplate) RenderString

func (s *XTemplate) RenderString(tplStr string, data interface{}) (string, error)

RenderString renders a template from a string. supports the extend and include actions

Jump to

Keyboard shortcuts

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