Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Eun/yaegi-template
Use yaegi as a template engine.
https://github.com/Eun/yaegi-template
go golang hacktoberfest template-engine yaegi yaegi-template
Last synced: about 2 months ago
JSON representation
Use yaegi as a template engine.
- Host: GitHub
- URL: https://github.com/Eun/yaegi-template
- Owner: Eun
- License: mit
- Archived: true
- Created: 2019-07-30T11:15:56.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-06-13T11:05:58.000Z (over 1 year ago)
- Last Synced: 2024-07-27T04:32:54.987Z (about 2 months ago)
- Topics: go, golang, hacktoberfest, template-engine, yaegi, yaegi-template
- Language: Go
- Homepage:
- Size: 1020 KB
- Stars: 32
- Watchers: 3
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# yaegi-template [![Actions Status](https://github.com/Eun/yaegi-template/workflows/CI/badge.svg)](https://github.com/Eun/yaegi-template/actions) [![Coverage Status](https://coveralls.io/repos/github/Eun/yaegi-template/badge.svg)](https://coveralls.io/github/Eun/yaegi-template) [![PkgGoDev](https://img.shields.io/badge/pkg.go.dev-reference-blue)](https://pkg.go.dev/github.com/Eun/yaegi-template) [![GoDoc](https://godoc.org/github.com/Eun/yaegi-template?status.svg)](https://godoc.org/github.com/Eun/yaegi-template) [![go-report](https://goreportcard.com/badge/github.com/Eun/yaegi-template)](https://goreportcard.com/report/github.com/Eun/yaegi-template)
Use [yaegi](https://github.com/traefik/yaegi) as a template engine.```go
package mainimport (
"os""github.com/Eun/yaegi-template"
)func main() {
template := yaegi_template.MustNew(yaegi_template.DefaultOptions(), yaegi_template.DefaultSymbols()...)
template.MustParseString(`<$
import (
"fmt"
"time"
)
func GreetUser(name string) {
fmt.Printf("Hello %s, it is %s", name, time.Now().Format(time.Kitchen))
}
$>
<$
if context.LoggedIn {
GreetUser(context.UserName)
}
$>`)
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.
```go
package mainimport (
"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(`
<$-
context.UserName
-$>`)
type Context struct {
LoggedIn bool
UserName string
}template.MustExec(os.Stdout, &Context{
LoggedIn: true,
UserName: "Joe Doe",
})
}
```