view.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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/session"
  10. "miniflux.app/v2/internal/ui/static"
  11. )
  12. // view wraps template argument building.
  13. type view struct {
  14. tpl *template.Engine
  15. r *http.Request
  16. params map[string]any
  17. }
  18. // Set adds a new template argument.
  19. func (v *view) Set(param string, value any) *view {
  20. v.params[param] = value
  21. return v
  22. }
  23. // Render executes the template with arguments.
  24. func (v *view) Render(template string) []byte {
  25. return v.tpl.Render(template+".html", v.params)
  26. }
  27. // New returns a new view with default parameters.
  28. func New(tpl *template.Engine, r *http.Request, sess *session.Session) *view {
  29. theme := request.UserTheme(r)
  30. return &view{tpl, r, map[string]any{
  31. "menu": "",
  32. "csrf": request.CSRF(r),
  33. "flashMessage": sess.FlashMessage(request.FlashMessage(r)),
  34. "flashErrorMessage": sess.FlashErrorMessage(request.FlashErrorMessage(r)),
  35. "theme": theme,
  36. "language": request.UserLanguage(r),
  37. "theme_checksum": static.StylesheetBundles[theme].Checksum,
  38. "app_js_checksum": static.JavascriptBundles["app"].Checksum,
  39. "sw_js_checksum": static.JavascriptBundles["service-worker"].Checksum,
  40. "webAuthnEnabled": config.Opts.WebAuthn(),
  41. }}
  42. }