slim

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2022 License: MIT Imports: 15 Imported by: 9

README

go-slim

Build Status Codecov Go Reference Go Report Card

slim template engine for golang

Features

  • Small Virtual Machine

    Enough to manipulate object in template. Support Number/String/Function/Array/Map.

  • Ruby Like Text Rendering

    Support Hello #{"Golang"}

Usage

Template File
doctype 5
html lang="ja"
  head
    meta charset="UTF-8"
    title
  body
    ul
    - for x in foo
      li = x
Your Code
tmpl, err := slim.ParseFile("template.slim")
if err != nil {
	t.Fatal(err)
}
err = tmpl.Execute(os.Stdout, slim.Values{
	"foo": []string{"foo", "bar", "baz"},
})
Output
<!doctype html>
<html lang="ja">
  <head>
    <meta charset="UTF-8"/>
    <title>
    </title>
  </head>
  <body>
    <ul>
      <li>foo</li>
      <li>bar</li>
      <li>baz</li>
    </ul>
  </body>
</html>

Builtin-Functions

  • trim(s)
  • to_upper(s)
  • to_lower(s)
  • repeat(s, n)

License

MIT

Author

Yasuhiro Matsumoto (a.k.a. mattn)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attr

type Attr struct {
	Name  string
	Value string
}

Attr is a type for indiacating attribute of tag.

type Func

type Func func(...Value) (Value, error)

Func is a type for indicating function for expression.

type Funcs

type Funcs map[string]Func

Funcs is a type for indicating function map to pass FuncMap().

type Node

type Node struct {
	Name     string
	ID       string
	Class    []string
	Attr     []Attr
	Text     string
	Expr     string
	Children []*Node
	Raw      bool
	Indent   int
}

Node is a type for indicating tag.

func (*Node) NewChild

func (n *Node) NewChild() *Node

NewChild create child node.

type Renderer

type Renderer func(out io.Writer, n *Node, v *vm.VM) error

Renderer is a type for indicating custom function for renderer.

type Template

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

Template is the representation of a parsed template.

func Parse

func Parse(in io.Reader) (*Template, error)

Parse parse content with reading from reader.

func ParseFile

func ParseFile(fname string) (*Template, error)

ParseFile parse content of fname.

func (*Template) Execute

func (t *Template) Execute(out io.Writer, value interface{}) error

Execute applies a parsed template to the specified value object, and writes the output to out.

func (*Template) FuncMap

func (t *Template) FuncMap(m Funcs)

FuncMap set the template's function map.

func (*Template) RegisterRenderer

func (t *Template) RegisterRenderer(name string, r Renderer)

RegisterRenderer register custom render named with the name.

type Value

type Value interface{}

Value is a type for indicating values for expression.

func Repeat

func Repeat(args ...Value) (Value, error)

Repeat is builtin function provide repeat(s, n).

func ToLower

func ToLower(args ...Value) (Value, error)

ToLower is builtin function provide to_lower(s).

func ToUpper

func ToUpper(args ...Value) (Value, error)

ToUpper is builtin function provide to_upper(s).

func Trim

func Trim(args ...Value) (Value, error)

Trim is builtin function provide trim(s).

type Values

type Values map[string]Value

Values is a collection of Value

Directories

Path Synopsis
_example
webapp Module
cmd

Jump to

Keyboard shortcuts

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