socks

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: MIT Imports: 13 Imported by: 0

README

Socks

Socks is a templating library for Go. It is made to be simple and easy to use. It provides easy ways to extend and include templates, it comes with a custom bytecode expression evaluator, static evaluation and more nice-to-haves.

Getting started

Installation
go get github.com/terawatthour/socks
Usage
<!-- templates/index.html -->
<html>
    <head>
        <title>Hello from {{ server }}</title>
        {! scripts !}
    </head>
    <body>
        <h1>Hello {{ name }}</h1>
        <p>The current time is {{ currentTime() }}</p>
    </body>
</html>
package main

import (
    "fmt"
    "time"
    "github.com/terawatthour/socks"
)

func main() {
    s := socks.NewSocks(&socks.Options{
        Sanitizer: func(s string) string {
            // Sanitize your output here, everything that
            // goes through the {{ ... }} tag is going to be sanitized 
            return s
        },
    })
	
    if err := s.LoadTemplates("templates/*.html"); err != nil {
        panic(err)
    }

    if err := s.Compile(map[string]any{
        "Server": "localhost",
    }); err != nil {
        panic(err)
    }

    s.AddGlobal("now", "2019-01-01")
	
    s.AddGlobal("currentTime", func() string {
        return time.Now().Format("2006-01-02 15:04:05")
    })
	
    result, err := s.ExecuteToString("templates/index.html", map[string]any{
        "name": "World",
        "currentTime": func() string {
            return time.Now().Format("01-02-2006 15:04:05")
        },
    })
    if err != nil {
        panic(err)
    }

    fmt.Println(result)
}

Elements

Escaped expression

Value of this expression will be printed to the template. It will be sanitized if a sanitizer function is provided.

{{ Users[0].CreatedAt.Format("2006-01-02") }}
Unescaped expression

Value of this expression will be printed to the template without any sanitization.

{! client.scripts !}
Preprocessor statements
  • @extend, @slot and @define are used for extending templates.
<!--base.html-->
<html>
    <head>
        <title>{! title !}</title>
    </head>
    <body>
        @slot("content")
        <h2>404 – Content not found</h2>
        @endslot
    </body>
</html>
@extend("base.html") <!-- extends the base.html template -->

@define("content")
    <!-- defines a block named content, 
         if omitted – template will fallback to default slot value 
         -->
    <h1>Hello World</h1>
@enddefine
  • @template, @slot and @define are used for including templates.
<!--header.html-->
<header>
    @slot("content") Fallback content @endslot
</header>
@template("header.html")
    @define("content")
        <!-- defines a block named content, 
             if omitted – template will fallback to default slot value 
             -->
        <h1>Hello World</h1>
    @enddefine
@endtemplate
Comment

Comment tag, the content of this tag will be ignored.

{# This is a comment #}
<!-- this is an HTML comment and it won't be removed -->
For statement
@for(user in Users)
<p>{{ user.ID }} – {{ user.Name }}</p>
@endfor

@for(user in Users with i)
<p>{{ i }}: {{ user.Name }}</p>
@endfor
If statement
@if(len(Users) == 1)
<p>{{ Users[0].Name }}</p>
@elif(len(Users) > 1)
<p>{{ Users[0].Name }} and {{ len(Users)-1 }} more...</p>
@else
<p>No users in the database</p>
@endif

Roadmap

  • Runtime error handling
  • Inline functions like map, filter, reduce, etc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Context added in v0.0.9

type Context = map[string]any

type DefineStatement added in v0.0.9

type DefineStatement struct {
	Name string

	Parent       Statement
	EndStatement *EndStatement
	// contains filtered or unexported fields
}

func (*DefineStatement) IsClosable added in v0.0.9

func (es *DefineStatement) IsClosable() bool

func (*DefineStatement) Kind added in v0.0.9

func (es *DefineStatement) Kind() string

func (*DefineStatement) Location added in v0.0.9

func (es *DefineStatement) Location() helpers.Location

type ElifStatement added in v0.0.9

type ElifStatement struct {
	Program *expression.VM
	// contains filtered or unexported fields
}

func (*ElifStatement) IsClosable added in v0.0.9

func (vs *ElifStatement) IsClosable() bool

func (*ElifStatement) Kind added in v0.0.9

func (vs *ElifStatement) Kind() string

func (*ElifStatement) Location added in v0.0.9

func (vs *ElifStatement) Location() helpers.Location

type ElseStatement added in v0.0.9

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

func (*ElseStatement) IsClosable added in v0.0.9

func (vs *ElseStatement) IsClosable() bool

func (*ElseStatement) Kind added in v0.0.9

func (vs *ElseStatement) Kind() string

func (*ElseStatement) Location added in v0.0.9

func (vs *ElseStatement) Location() helpers.Location

type EndStatement added in v0.0.9

type EndStatement struct {
	ClosedStatement Statement
	// contains filtered or unexported fields
}

func (*EndStatement) IsClosable added in v0.0.9

func (es *EndStatement) IsClosable() bool

func (*EndStatement) Kind added in v0.0.9

func (es *EndStatement) Kind() string

func (*EndStatement) Location added in v0.0.9

func (es *EndStatement) Location() helpers.Location

type Evaluable added in v0.0.9

type Evaluable interface {
	Statement
	Dependencies() []string
	Evaluate(evaluator *evaluator, context Context) error
}

type Expression added in v0.0.9

type Expression struct {
	Program *expression.VM
	// contains filtered or unexported fields
}

func (*Expression) Dependencies added in v0.0.9

func (expr *Expression) Dependencies() []string

func (*Expression) Evaluate added in v0.0.9

func (expr *Expression) Evaluate(e *evaluator, context Context) (err error)

func (*Expression) IsClosable added in v0.0.9

func (expr *Expression) IsClosable() bool

func (*Expression) Kind added in v0.0.9

func (expr *Expression) Kind() string

func (*Expression) Location added in v0.0.9

func (expr *Expression) Location() helpers.Location

type ExtendStatement added in v0.0.9

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

func (*ExtendStatement) IsClosable added in v0.0.9

func (es *ExtendStatement) IsClosable() bool

func (*ExtendStatement) Kind added in v0.0.9

func (es *ExtendStatement) Kind() string

func (*ExtendStatement) Location added in v0.0.9

func (es *ExtendStatement) Location() helpers.Location

type ForStatement added in v0.0.9

type ForStatement struct {
	Iterable  *expression.VM
	KeyName   string
	ValueName string

	EndStatement *EndStatement
	// contains filtered or unexported fields
}

func (*ForStatement) Dependencies added in v0.0.9

func (st *ForStatement) Dependencies() []string

func (*ForStatement) Evaluate added in v0.0.9

func (st *ForStatement) Evaluate(e *evaluator, context Context) error

func (*ForStatement) IsClosable added in v0.0.9

func (st *ForStatement) IsClosable() bool

func (*ForStatement) Kind added in v0.0.9

func (st *ForStatement) Kind() string

func (*ForStatement) Location added in v0.0.9

func (st *ForStatement) Location() helpers.Location

func (*ForStatement) String added in v0.0.9

func (st *ForStatement) String() string

type IfStatement added in v0.0.9

type IfStatement struct {
	Program *expression.VM

	ElifStatements []Statement
	ElseStatement  Statement
	EndStatement   Statement
	// contains filtered or unexported fields
}

func (*IfStatement) Dependencies added in v0.0.9

func (st *IfStatement) Dependencies() []string

func (*IfStatement) Evaluate added in v0.0.9

func (st *IfStatement) Evaluate(e *evaluator, context Context) error

func (*IfStatement) IsClosable added in v0.0.9

func (st *IfStatement) IsClosable() bool

func (*IfStatement) Kind added in v0.0.9

func (st *IfStatement) Kind() string

func (*IfStatement) Location added in v0.0.9

func (st *IfStatement) Location() helpers.Location

func (*IfStatement) String added in v0.0.9

func (st *IfStatement) String() string

type Options added in v0.0.6

type Options struct {
	Sanitizer func(string) string
}

type Preprocessor added in v0.0.9

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

func New added in v0.0.9

func New(files map[string]string, nativeMap map[string]string, staticContext map[string]interface{}, sanitizer func(string) string) *Preprocessor

func (*Preprocessor) Preprocess added in v0.0.9

func (p *Preprocessor) Preprocess(filename string, keepSlots bool) ([]Statement, error)

type SlotStatement added in v0.0.9

type SlotStatement struct {
	Name string

	Parent       Statement
	EndStatement *EndStatement
	// contains filtered or unexported fields
}

func (*SlotStatement) IsClosable added in v0.0.9

func (ss *SlotStatement) IsClosable() bool

func (*SlotStatement) Kind added in v0.0.9

func (ss *SlotStatement) Kind() string

func (*SlotStatement) Location added in v0.0.9

func (ss *SlotStatement) Location() helpers.Location

type Socks

type Socks interface {
	ExecuteToString(template string, context map[string]interface{}) (string, error)
	Execute(w io.Writer, template string, context map[string]interface{}) error

	LoadTemplates(pattern string, removePrefix ...string) error
	LoadTemplateFromString(filename string, content string)
	Compile(staticContext map[string]interface{}) error

	AddGlobal(key string, value interface{})
	AddGlobals(value map[string]interface{})
	GetGlobals() map[string]interface{}
	ClearGlobals()
}

func NewSocks

func NewSocks(options ...*Options) Socks

type Statement added in v0.0.9

type Statement interface {
	Kind() string
	Location() helpers.Location

	// IsClosable returns true if the statement is supposed to be closed with an `@end...` statement
	IsClosable() bool
}

func Parse added in v0.0.9

func Parse(file helpers.File, elements []tokenizer.Element) ([]Statement, error)

type TemplateStatement added in v0.0.9

type TemplateStatement struct {
	Template string

	EndStatement *EndStatement
	// contains filtered or unexported fields
}

func (*TemplateStatement) IsClosable added in v0.0.9

func (es *TemplateStatement) IsClosable() bool

func (*TemplateStatement) Kind added in v0.0.9

func (es *TemplateStatement) Kind() string

func (*TemplateStatement) Location added in v0.0.9

func (es *TemplateStatement) Location() helpers.Location

type Text added in v0.0.9

type Text struct {
	Content string
}

func (*Text) IsClosable added in v0.0.9

func (t *Text) IsClosable() bool

func (*Text) Kind added in v0.0.9

func (t *Text) Kind() string

func (*Text) Location added in v0.0.9

func (t *Text) Location() helpers.Location

Directories

Path Synopsis
internal
examples/blog command

Jump to

Keyboard shortcuts

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