yaegi_template

package module
v1.5.19 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2021 License: MIT Imports: 16 Imported by: 3

README

yaegi-template Actions Status Coverage Status PkgGoDev GoDoc go-report

Use yaegi as a template engine.

package main

import (
	"os"

	"github.com/Eun/yaegi-template"
)

func main() {
	template := yaegi_template.MustNew(yaegi_template.DefaultOptions(), yaegi_template.DefaultSymbols()...)
	template.MustParseString(`
<html>
<$
    import (
        "fmt"
        "time"
    )
    func GreetUser(name string) {
        fmt.Printf("Hello %s, it is %s", name, time.Now().Format(time.Kitchen))
    }
$>

<p>
<$
    if context.LoggedIn {
        GreetUser(context.UserName)
    }
$>
</p>
</html>
`)

    type Context struct {
        LoggedIn bool
        UserName string
    }

    template.MustExec(os.Stdout, &Context{
        LoggedIn: true,
        UserName: "Joe Doe",
    })
}

Example #2

You can use <$- to strip white spaces before the code block and -$> to strip white spaces after the code block.
Also omitting the print statement for simple evaluations is possible.

package main

import (
	"os"

	"github.com/Eun/yaegi-template"
	"github.com/traefik/yaegi/interp"
	"github.com/traefik/yaegi/stdlib"
)

func main() {
	template := yaegi_template.MustNew(interp.Options{}, stdlib.Symbols)
	template.MustParseString(`
<html>
<p>
<$-
    context.UserName
-$>
</html>
`)

	type Context struct {
		LoggedIn bool
		UserName string
	}

	template.MustExec(os.Stdout, &Context{
		LoggedIn: true,
		UserName: "Joe Doe",
	})
}

Documentation

Overview

Package yaegi_template is a package that provides a templating engine using yeagi. yaegi is a golang interpreter and can be used to run go code inside an go application.

Example usage:

package main

import (
    "os"

    "github.com/Eun/yaegi-template"
)

func main() {
    template := yaegi_template.MustNew(yaegi_template.DefaultOptions(), yaegi_template.DefaultSymbols()...)
    template.MustParseString(`
<html>
<$
    import "time"
    func GreetUser(name string) {
        fmt.Printf("Hello %s, it is %s", name, time.Now().Format(time.Kitchen))
    }
$>

<p>
<$
    if context.LoggedIn {
        GreetUser(context.UserName)
    }
$>
</p>
</html>
`)

    type Context struct {
        LoggedIn bool
        UserName string
    }

    template.MustExec(os.Stdout, &Context{
        LoggedIn: true,
        UserName: "Joe Doe",
    })
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultOptions added in v1.5.11

func DefaultOptions() interp.Options

DefaultOptions return the default options for the New and MustNew functions.

func DefaultSymbols added in v1.5.13

func DefaultSymbols() []interp.Exports

DefaultSymbols return the default symbols for the New and MustNew functions.

Types

type Import added in v1.5.13

type Import struct {
	Name string
	Path string
}

Import represents an import that should be evaluated.

func (Import) Equals added in v1.5.13

func (v Import) Equals(i Import) bool

Equals returns true if the specified import is equal to this import.

type Template

type Template struct {
	StartTokens []rune
	EndTokens   []rune
	// contains filtered or unexported fields
}

Template represents a template.

func MustNew

func MustNew(
	options interp.Options,
	use ...interp.Exports) *Template

MustNew is like New, except it panics on failure.

func New

func New(
	options interp.Options,
	use ...interp.Exports) (*Template, error)

New creates a new Template that can be used in a later time.

func (*Template) Exec

func (t *Template) Exec(writer io.Writer, context interface{}) (int, error)

Exec executes the template, and writes the output to the specified writer.

func (*Template) Import added in v1.5.13

func (t *Template) Import(imports ...Import) error

Import imports the specified imports to the interpreter.

func (*Template) LazyParse added in v1.5.11

func (t *Template) LazyParse(reader io.Reader) error

LazyParse parses the specified reader during usage of Exec().

func (*Template) MustExec

func (t *Template) MustExec(writer io.Writer, context interface{})

MustExec is like Exec, except it panics on failure.

func (*Template) MustImport added in v1.5.13

func (t *Template) MustImport(imports ...Import) *Template

MustImport is like Import, except it panics on failure.

func (*Template) MustLazyParse added in v1.5.11

func (t *Template) MustLazyParse(r io.Reader) *Template

MustLazyParse is like LazyParse, except it panics on failure.

func (*Template) MustParse

func (t *Template) MustParse(r io.Reader) *Template

MustParse is like Parse, except it panics on failure.

func (*Template) MustParseBytes added in v1.5.11

func (t *Template) MustParseBytes(b []byte) error

MustParseBytes is like ParseBytes, except it panics on failure.

func (*Template) MustParseString

func (t *Template) MustParseString(s string) *Template

MustParseString is like ParseString, except it panics on failure.

func (*Template) MustUse added in v1.5.15

func (t *Template) MustUse(values ...interp.Exports) *Template

MustUse is like Use, except it panics on failure.

func (*Template) Parse

func (t *Template) Parse(reader io.Reader) error

Parse parses the specified reader, after success it is possible to call Exec() on the template.

func (*Template) ParseBytes added in v1.5.11

func (t *Template) ParseBytes(b []byte) error

ParseBytes parses the specified byte slice, after success it is possible to call Exec() on the template.

func (*Template) ParseString

func (t *Template) ParseString(s string) error

ParseString parses the specified string, after success it is possible to call Exec() on the template.

func (*Template) Use added in v1.5.15

func (t *Template) Use(values ...interp.Exports) error

Use loads binary runtime symbols in the interpreter context so they can be used in interpreted code.

Directories

Path Synopsis
Package codebuffer is a package that provides a method to read trough a reader and separate the contents into code and text parts.
Package codebuffer is a package that provides a method to read trough a reader and separate the contents into code and text parts.
examples
evaluate_readme
read README.md and find all “`go “` snippets and run them
read README.md and find all “`go “` snippets and run them
render_readme
read README.md.tmpl and find all “`go “` snippets and run them
read README.md.tmpl and find all “`go “` snippets and run them

Jump to

Keyboard shortcuts

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