view.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/config"
  7. "miniflux.app/v2/internal/http/request"
  8. "miniflux.app/v2/internal/template"
  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]any
  16. }
  17. // Set adds a new template argument.
  18. func (v *view) Set(param string, value any) *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) *view {
  28. webSession := request.WebSession(r)
  29. flashSuccessMessage, flashErrorMessage := webSession.ConsumeMessages()
  30. v := newView(tpl, r)
  31. v.params["csrf"] = webSession.CSRF()
  32. v.params["flashSuccessMessage"] = flashSuccessMessage
  33. v.params["flashErrorMessage"] = flashErrorMessage
  34. return v
  35. }
  36. // NewPublic returns a view for public pages, without the CSRF token or flash messages.
  37. func NewPublic(tpl *template.Engine, r *http.Request) *view {
  38. return newView(tpl, r)
  39. }
  40. func newView(tpl *template.Engine, r *http.Request) *view {
  41. webSession := request.WebSession(r)
  42. theme := webSession.Theme()
  43. return &view{tpl, r, map[string]any{
  44. "menu": "",
  45. "theme": theme,
  46. "language": webSession.Language(),
  47. "theme_checksum": static.StylesheetBundles[theme+".css"].Checksum,
  48. "app_js_checksum": static.JavascriptBundles["app.js"].Checksum,
  49. "sw_js_checksum": static.JavascriptBundles["service-worker.js"].Checksum,
  50. "webAuthnEnabled": config.Opts.WebAuthn(),
  51. }}
  52. }