view.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package view // import "miniflux.app/v2/internal/ui/view"
  4. import (
  5. "net/http"
  6. "miniflux.app/v2/internal/http/request"
  7. "miniflux.app/v2/internal/template"
  8. "miniflux.app/v2/internal/ui/session"
  9. "miniflux.app/v2/internal/ui/static"
  10. )
  11. // View wraps template argument building.
  12. type View struct {
  13. tpl *template.Engine
  14. r *http.Request
  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+".html", v.params)
  25. }
  26. // New returns a new view with default parameters.
  27. func New(tpl *template.Engine, r *http.Request, sess *session.Session) *View {
  28. b := &View{tpl, r, make(map[string]interface{})}
  29. theme := request.UserTheme(r)
  30. b.params["menu"] = ""
  31. b.params["csrf"] = request.CSRF(r)
  32. b.params["flashMessage"] = sess.FlashMessage(request.FlashMessage(r))
  33. b.params["flashErrorMessage"] = sess.FlashErrorMessage(request.FlashErrorMessage(r))
  34. b.params["theme"] = theme
  35. b.params["language"] = request.UserLanguage(r)
  36. b.params["theme_checksum"] = static.StylesheetBundleChecksums[theme]
  37. b.params["app_js_checksum"] = static.JavascriptBundleChecksums["app"]
  38. b.params["sw_js_checksum"] = static.JavascriptBundleChecksums["service-worker"]
  39. return b
  40. }