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: 3 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 (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-06-13T11:05:58.000Z (about 2 years ago)
- Last Synced: 2024-11-05T21:45:37.269Z (8 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 [](https://github.com/Eun/yaegi-template/actions) [](https://coveralls.io/github/Eun/yaegi-template) [](https://pkg.go.dev/github.com/Eun/yaegi-template) [](https://godoc.org/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",
})
}
```