yaegi_template

package module
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2020 License: MIT Imports: 13 Imported by: 3

README

yaegi-template Actions Status Codecov GoDoc go-report

Use yaegi as a template engine.

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>
<$
	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",
	})
}

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RuneReader

type RuneReader interface {
	ReadRune() (rune, int, error)
}

type Template

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

func MustNew

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

func New

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

func (*Template) Exec

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

func (*Template) MustExec

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

func (*Template) MustParse

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

func (*Template) MustParseString

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

func (*Template) Parse

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

func (*Template) ParseString

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

Directories

Path Synopsis
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