aboutsummaryrefslogblamecommitdiff
path: root/docs/example-go-plugin/plugin.go
blob: 7cf2eb9c54320a8b6b67f168ad8126b305ecf9fe (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                          
  
                                                           





                     

                                                        


             
                                           

                                                                    

                                                                                       






                                                                                               
                                                         









                                                                            
                                             
 
// Package exampleplugin is an example Go plugin for alps.
//
// To enable it, import this package from cmd/alps/main.go.
package exampleplugin

import (
	"fmt"
	"net/http"

	"git.sr.ht/~emersion/alps"
	alpsbase "git.sr.ht/~emersion/alps/plugins/base"
)

func init() {
	p := alps.GoPlugin{Name: "example"}

	// Setup a function called when the mailbox view is rendered
	p.Inject("mailbox.html", func(ctx *alps.Context, kdata alps.RenderData) error {
		data := kdata.(*alpsbase.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 *alps.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
		},
	})

	alps.RegisterPluginLoader(p.Loader())
}