view.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. theme := webSession.Theme()
  30. flashSuccessMessage, flashErrorMessage := webSession.ConsumeMessages()
  31. return &view{tpl, r, map[string]any{
  32. "menu": "",
  33. "csrf": webSession.CSRF(),
  34. "flashSuccessMessage": flashSuccessMessage,
  35. "flashErrorMessage": flashErrorMessage,
  36. "theme": theme,
  37. "language": webSession.Language(),
  38. "theme_checksum": static.StylesheetBundles[theme+".css"].Checksum,
  39. "app_js_checksum": static.JavascriptBundles["app.js"].Checksum,
  40. "sw_js_checksum": static.JavascriptBundles["service-worker.js"].Checksum,
  41. "webAuthnEnabled": config.Opts.WebAuthn(),
  42. }}
  43. }