aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2020-02-11 18:23:21 +0100
committerSimon Ser <contact@emersion.fr>2020-02-11 18:23:48 +0100
commite59ad57e32289ef62e17a8361b6d36b77e56a25d (patch)
tree95352c446227afd1bf380d25cd2d37082beaebbd /docs
parente09a83756922a2c7855c0b7159fdd12b87153cd4 (diff)
downloadalps-e59ad57e32289ef62e17a8361b6d36b77e56a25d.tar.gz
alps-e59ad57e32289ef62e17a8361b6d36b77e56a25d.zip
docs: add an example Go plugin
Diffstat (limited to 'docs')
-rw-r--r--docs/example-go-plugin/plugin.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/docs/example-go-plugin/plugin.go b/docs/example-go-plugin/plugin.go
new file mode 100644
index 0000000..791f5a6
--- /dev/null
+++ b/docs/example-go-plugin/plugin.go
@@ -0,0 +1,39 @@
+// Package exampleplugin is an example Go plugin for koushin.
+//
+// To enable it, import this package from cmd/koushin/main.go.
+package exampleplugin
+
+import (
+ "fmt"
+ "net/http"
+
+ "git.sr.ht/~emersion/koushin"
+ koushinbase "git.sr.ht/~emersion/koushin/plugins/base"
+)
+
+func init() {
+ p := koushin.GoPlugin{Name: "example"}
+
+ // Setup a function called when the mailbox view is rendered
+ p.Inject("mailbox.html", func(ctx *koushin.Context, kdata koushin.RenderData) error {
+ data := kdata.(*koushinbase.MailboxRenderData)
+ 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
+ p.GET("/example", func(ctx *koushin.Context) error {
+ 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
+ },
+ })
+
+ koushin.RegisterPluginLoader(p.Loader())
+}