view.go 1.6 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/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]interface{}
  17. }
  18. // Set adds a new template argument.
  19. func (v *View) Set(param string, value interface{}) *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]interface{}{
  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.StylesheetBundleChecksums[theme],
  38. "app_js_checksum": static.JavascriptBundleChecksums["app"],
  39. "sw_js_checksum": static.JavascriptBundleChecksums["service-worker"],
  40. "webauthn_js_checksum": static.JavascriptBundleChecksums["webauthn"],
  41. "webAuthnEnabled": config.Opts.WebAuthn(),
  42. }}
  43. }