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 ¶
- func DefaultOptions() interp.Options
- func DefaultSymbols() []interp.Exports
- type Import
- type Template
- func (t *Template) Exec(writer io.Writer, context interface{}) (int, error)
- func (t *Template) Import(imports ...Import) error
- func (t *Template) LazyParse(reader io.Reader) error
- func (t *Template) MustExec(writer io.Writer, context interface{})
- func (t *Template) MustImport(imports ...Import) *Template
- func (t *Template) MustLazyParse(r io.Reader) *Template
- func (t *Template) MustParse(r io.Reader) *Template
- func (t *Template) MustParseBytes(b []byte) error
- func (t *Template) MustParseString(s string) *Template
- func (t *Template) Parse(reader io.Reader) error
- func (t *Template) ParseBytes(b []byte) error
- func (t *Template) ParseString(s string) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultOptions ¶ added in v1.5.11
DefaultOptions return the default options for the New and MustNew functions.
func DefaultSymbols ¶ added in v1.5.13
DefaultSymbols return the default symbols for the New and MustNew functions.
Types ¶
type Template ¶
type Template struct {
StartTokens []rune
EndTokens []rune
// contains filtered or unexported fields
}
Template represents a template.
func (*Template) LazyParse ¶ added in v1.5.11
LazyParse parses the specified reader during usage of Exec().
func (*Template) MustImport ¶ added in v1.5.13
MustImport is like Import, except it panics on failure.
func (*Template) MustLazyParse ¶ added in v1.5.11
MustLazyParse is like LazyParse, except it panics on failure.
func (*Template) MustParseBytes ¶ added in v1.5.11
MustParseBytes is like ParseBytes, except it panics on failure.
func (*Template) MustParseString ¶
MustParseString is like ParseString, except it panics on failure.
func (*Template) Parse ¶
Parse parses the specified reader, after success it is possible to call Exec() on the template.
func (*Template) ParseBytes ¶ added in v1.5.11
ParseBytes parses the specified byte slice, after success it is possible to call Exec() on the template.
func (*Template) ParseString ¶
ParseString parses the specified string, after success it is possible to call Exec() on the template.
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
command
read README.md and find all “`go “` snippets and run them
|
read README.md and find all “`go “` snippets and run them |
|
render_readme
command
read README.md.tmpl and find all “`go “` snippets and run them
|
read README.md.tmpl and find all “`go “` snippets and run them |
|
simple
command
|