view.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package view
  5. import (
  6. "github.com/miniflux/miniflux/http/context"
  7. "github.com/miniflux/miniflux/template"
  8. "github.com/miniflux/miniflux/ui/session"
  9. )
  10. // View wraps template argument building.
  11. type View struct {
  12. tpl *template.Engine
  13. ctx *context.Context
  14. params map[string]interface{}
  15. }
  16. // Set adds a new template argument.
  17. func (v *View) Set(param string, value interface{}) *View {
  18. v.params[param] = value
  19. return v
  20. }
  21. // Render executes the template with arguments.
  22. func (v *View) Render(template string) []byte {
  23. return v.tpl.Render(template, v.ctx.UserLanguage(), v.params)
  24. }
  25. // New returns a new view with default parameters.
  26. func New(tpl *template.Engine, ctx *context.Context, sess *session.Session) *View {
  27. b := &View{tpl, ctx, make(map[string]interface{})}
  28. b.params["menu"] = ""
  29. b.params["csrf"] = ctx.CSRF()
  30. b.params["flashMessage"] = sess.FlashMessage()
  31. b.params["flashErrorMessage"] = sess.FlashErrorMessage()
  32. return b
  33. }