templates.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package fiber
  2. import (
  3. "errors"
  4. "github.com/gofiber/fiber/v2"
  5. "github.com/gofiber/template/html/v2"
  6. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
  7. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/templates/functions"
  8. )
  9. func getViewsEngine(sp ServiceProvider) *html.Engine {
  10. engine := html.New(templatesPath, ".html")
  11. engine.AddFunc("now", functions.Now)
  12. engine.AddFunc("noescape", functions.NoEscape)
  13. engine.AddFunc("concat", functions.Concat)
  14. engine.AddFunc("gridsep", functions.GridSep)
  15. engine.AddFunc("version", functions.Version(sp))
  16. engine.AddFunc("trans", functions.Trans)
  17. engine.AddFunc("link", functions.Link)
  18. return engine
  19. }
  20. func getErrorHandler() fiber.ErrorHandler {
  21. return func(fctx *fiber.Ctx, err error) error {
  22. lang := i18n.LanguageFromContext(fctx)
  23. errCode := fiber.StatusInternalServerError
  24. var e *fiber.Error
  25. if errors.As(err, &e) {
  26. errCode = e.Code
  27. }
  28. var renderData fiber.Map
  29. if errCode == fiber.StatusNotFound {
  30. renderData = fiber.Map{
  31. "pageTitle": i18n.T(lang, "page_not_found_err_title"),
  32. "code": fiber.StatusNotFound,
  33. "text": i18n.T(lang, "page_not_found_err_desc"),
  34. }
  35. } else {
  36. renderData = fiber.Map{
  37. "pageTitle": i18n.T(lang, "internal_err_title"),
  38. "code": fiber.StatusInternalServerError,
  39. "text": i18n.T(lang, "internal_err_desc"),
  40. }
  41. }
  42. renderData["headTitle"] = i18n.T(lang, "head_title")
  43. return fctx.Render("error", renderData, "_layout")
  44. }
  45. }