I don't know how to use the templates plugin correctly

I want to use dynamic template rendering. According to the information, I should use the templates plugin, but I don’t know how to assign values to template fields in the code.
this is my index.html:

this is my caddyfile : {
root * /Users/sxf/Desktop/test/dist

I have written a plugin called userMiddleware, which is used to render dynamic templates, so should I assign values to template variables in this plugin? If so, how to assign a value to {{. Name}}

package user

type UserHandler struct {
User string

func init() {
httpcaddyfile.RegisterHandlerDirective(“userMiddleware”, parseMiddlewarefile)

func parseMiddlewarefile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
hw := new(UserHandler)
err := hw.UnmarshalCaddyfile(h.Dispenser)
return hw, err

func (m *UserHandler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
if !d.Args(&m.User) {
return d.ArgErr()
return nil

func (UserHandler) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: “http.handlers.userMiddleware”,
New: func() caddy.Module { return new(UserHandler) },
func (m UserHandler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {
// todo
// how to assign a value to {{. Name}}
return next.ServeHTTP(w, r)

Looking forward to your answer, thank you

You’ll want to use this package, probably:

You can set values in the caddy.Replacer and then user them later with the placeholder template function Modules - Caddy Documentation


I want to use custom variable names such as {{. GlobalName}}, {BackEndData}}, and I also want to use Caddy’s built-in plugin (templates) for rendering. Can this be achieved?

Of course; use the templates package I linked to, then (following the examples on that page) parse the template and evaluate it, and write its result to the ResponseWriter. This is essentially what the existing templates handler does.

But if you want to use the existing templates handler without re-implementing that logic, you should do what Francis suggested. You can also set variables: vars (Caddyfile directive) — Caddy Documentation

Solved, thank you very much for your help


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.