You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.go 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package templates
  5. import (
  6. "os"
  7. "strings"
  8. "time"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/util"
  12. "github.com/unrolled/render"
  13. )
  14. // Vars represents variables to be render in golang templates
  15. type Vars map[string]interface{}
  16. // Merge merges another vars to the current, another Vars will override the current
  17. func (vars Vars) Merge(another map[string]interface{}) Vars {
  18. for k, v := range another {
  19. vars[k] = v
  20. }
  21. return vars
  22. }
  23. // BaseVars returns all basic vars
  24. func BaseVars() Vars {
  25. var startTime = time.Now()
  26. return map[string]interface{}{
  27. "IsLandingPageHome": setting.LandingPageURL == setting.LandingPageHome,
  28. "IsLandingPageExplore": setting.LandingPageURL == setting.LandingPageExplore,
  29. "IsLandingPageOrganizations": setting.LandingPageURL == setting.LandingPageOrganizations,
  30. "ShowRegistrationButton": setting.Service.ShowRegistrationButton,
  31. "ShowMilestonesDashboardPage": setting.Service.ShowMilestonesDashboardPage,
  32. "ShowFooterBranding": setting.ShowFooterBranding,
  33. "ShowFooterVersion": setting.ShowFooterVersion,
  34. "EnableSwagger": setting.API.EnableSwagger,
  35. "EnableOpenIDSignIn": setting.Service.EnableOpenIDSignIn,
  36. "PageStartTime": startTime,
  37. "TmplLoadTimes": func() string {
  38. return time.Since(startTime).String()
  39. },
  40. }
  41. }
  42. func getDirAssetNames(dir string) []string {
  43. var tmpls []string
  44. f, err := os.Stat(dir)
  45. if err != nil {
  46. if os.IsNotExist(err) {
  47. return tmpls
  48. }
  49. log.Warn("Unable to check if templates dir %s is a directory. Error: %v", dir, err)
  50. return tmpls
  51. }
  52. if !f.IsDir() {
  53. log.Warn("Templates dir %s is a not directory.", dir)
  54. return tmpls
  55. }
  56. files, err := util.StatDir(dir)
  57. if err != nil {
  58. log.Warn("Failed to read %s templates dir. %v", dir, err)
  59. return tmpls
  60. }
  61. for _, filePath := range files {
  62. if strings.HasPrefix(filePath, "mail/") {
  63. continue
  64. }
  65. if !strings.HasSuffix(filePath, ".tmpl") {
  66. continue
  67. }
  68. tmpls = append(tmpls, "templates/"+filePath)
  69. }
  70. return tmpls
  71. }
  72. // HTMLRenderer returns a render.
  73. func HTMLRenderer() *render.Render {
  74. return render.New(render.Options{
  75. Extensions: []string{".tmpl"},
  76. Directory: "templates",
  77. Funcs: NewFuncMap(),
  78. Asset: GetAsset,
  79. AssetNames: GetAssetNames,
  80. IsDevelopment: !setting.IsProd(),
  81. DisableHTTPErrorRendering: true,
  82. })
  83. }