alps/docs/example-go-plugin/plugin.go

40 lines
1 KiB
Go
Raw Normal View History

2020-05-13 12:07:44 +00:00
// Package exampleplugin is an example Go plugin for alps.
2020-02-11 17:23:21 +00:00
//
2020-05-13 12:07:44 +00:00
// To enable it, import this package from cmd/alps/main.go.
2020-02-11 17:23:21 +00:00
package exampleplugin
import (
"fmt"
"net/http"
"git.sr.ht/~migadu/alps"
alpsbase "git.sr.ht/~migadu/alps/plugins/base"
2020-02-11 17:23:21 +00:00
)
func init() {
2020-05-13 12:07:44 +00:00
p := alps.GoPlugin{Name: "example"}
2020-02-11 17:23:21 +00:00
// Setup a function called when the mailbox view is rendered
2020-05-13 12:07:44 +00:00
p.Inject("mailbox.html", func(ctx *alps.Context, kdata alps.RenderData) error {
data := kdata.(*alpsbase.MailboxRenderData)
2020-02-11 17:23:21 +00:00
fmt.Println("The mailbox view for " + data.Mailbox.Name + " is being rendered")
// Set extra data that can be accessed from the mailbox.html template
data.Extra["Example"] = "Hi from Go"
return nil
})
// Wire up a new route
2020-05-13 12:07:44 +00:00
p.GET("/example", func(ctx *alps.Context) error {
2020-02-11 17:23:21 +00:00
return ctx.String(http.StatusOK, "This is an example page.")
})
// Register a helper function that can be called from templates
p.TemplateFuncs(map[string]interface{}{
"example_and": func(a, b string) string {
return a + " and " + b
},
})
2020-05-13 12:07:44 +00:00
alps.RegisterPluginLoader(p.Loader())
2020-02-11 17:23:21 +00:00
}