view.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. "github.com/miniflux/miniflux/ui/static"
  10. )
  11. // View wraps template argument building.
  12. type View struct {
  13. tpl *template.Engine
  14. ctx *context.Context
  15. params map[string]interface{}
  16. }
  17. // Set adds a new template argument.
  18. func (v *View) Set(param string, value interface{}) *View {
  19. v.params[param] = value
  20. return v
  21. }
  22. // Render executes the template with arguments.
  23. func (v *View) Render(template string) []byte {
  24. return v.tpl.Render(template, v.ctx.UserLanguage(), v.params)
  25. }
  26. // New returns a new view with default parameters.
  27. func New(tpl *template.Engine, ctx *context.Context, sess *session.Session) *View {
  28. b := &View{tpl, ctx, make(map[string]interface{})}
  29. theme := ctx.UserTheme()
  30. b.params["menu"] = ""
  31. b.params["csrf"] = ctx.CSRF()
  32. b.params["flashMessage"] = sess.FlashMessage()
  33. b.params["flashErrorMessage"] = sess.FlashErrorMessage()
  34. b.params["theme"] = theme
  35. b.params["theme_checksum"] = static.StylesheetsChecksums[theme]
  36. b.params["app_js_checksum"] = static.JavascriptsChecksums["app"]
  37. b.params["sw_js_checksum"] = static.JavascriptsChecksums["sw"]
  38. return b
  39. }