slim

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 17 Imported by: 12

README

go-slim

Build Status Codecov Go Reference Go Report Card

Slim template engine for Go.

Features

  • Slim-compatible syntax — indentation-based HTML templating
  • Expressions — arithmetic, comparison (==, !=, <, >, <=, >=), logical (&&, ||, !)
  • Conditionals — if / elsif / else / unless
  • Iteration — for loops over slices, arrays, and channels
  • Layouts — content_for / yield with SetLayout, matching Ruby Slim conventions
  • Embedded filesystem — ParseFS for use with embed.FS
  • String interpolation — Hello #{name} (HTML-escaped), #{{html}} (raw)
  • Custom renderers — built-in javascript: and css: blocks, extensible via RegisterRenderer
  • Whitespace control — > (outer) and < (inner)
  • Wrapped attributes — div(id="x" class="y")
  • Self-closing tags — div/

Usage

Basic
doctype 5
html lang="ja"
  head
    meta charset="UTF-8"
    title
  body
    ul
      - for x in foo
        li = x
tmpl, err := slim.ParseFile("template.slim")
if err != nil {
    log.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>
Conditionals
- if user.Role == "admin"
  p Welcome, admin!
- elsif user.Role == "mod"
  p Welcome, moderator!
- else
  p Hello!

- unless hidden
  p This is visible
Layouts

Define blocks in pages with content_for, render them in layouts with yield.

layout.slim:

doctype 5
html
  head
    title My App
  body
    = yield
    = yield sidebar

index.slim:

h1 Hello
p Main content

- content_for sidebar
  nav
    a href="/" Home

Go code:

tmpl, _ := slim.ParseFile("index.slim")
tmpl.SetLayout("layout.slim")
tmpl.Execute(w, values)
Embedded Filesystem
//go:embed templates/*
var templates embed.FS

tmpl, _ := slim.ParseFS(templates, "templates/index.slim")
tmpl.SetLayout("templates/layout.slim")
tmpl.Execute(w, values)
Tags
/ Code comment (not rendered)
/! HTML comment

div#main.container
div(id="main" class="container")

img/
custom-element/

p> Removes outer whitespace
p< Removes inner whitespace

| Verbatim text
= expression (HTML escaped, wrapped in div)
== expression (raw output)
- code (control flow, no output)
Expressions
= name
= user.Name
= items[0]
= score + 10
= price * 1.1
= count > 0 && active
= !hidden
= greet("world")
= item.Method(arg)
String Interpolation
p Hello #{name}
a href="#{base_url}/path" Link
p #{{trusted_html}}

#{...} output is HTML-escaped. Use #{{...}} to output the value without escaping.

Custom Renderers
javascript:
  var x = 1;

css:
  body { color: red; }

Register custom renderers:

tmpl.RegisterRenderer("my-lang", func(out io.Writer, n *slim.Node, v *vm.VM) error {
    fmt.Fprint(out, n.Text)
    return nil
})

Built-in Functions

Function Description
trim(s) Trim whitespace
to_upper(s) Convert to uppercase
to_lower(s) Convert to lowercase
repeat(s, n) Repeat string n times

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
	TrimOuter bool // > remove outer whitespace
	TrimInner bool // < remove inner whitespace
	SelfClose bool // / explicit self-closing tag
	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 ParseFS added in v0.0.5

func ParseFS(fsys fs.FS, name string) (*Template, error)

ParseFS parse content of name from fsys.

func ParseFile

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

ParseFile parse content of fname.

func (*Template) Execute

func (t *Template) Execute(out io.Writer, value any) 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.

func (*Template) SetLayout added in v0.0.5

func (t *Template) SetLayout(name string)

SetLayout set the layout template name.

type Value

type Value any

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
goslim command

Jump to

Keyboard shortcuts

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