page_layout.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package layouts
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/qor5/admin/pagebuilder"
  6. "github.com/qor5/web"
  7. . "github.com/theplant/htmlgo"
  8. )
  9. func DefaultPageLayoutFunc(body HTMLComponent, input *pagebuilder.PageLayoutInput, ctx *web.EventContext) HTMLComponent {
  10. var seoTags HTMLComponent
  11. if len(input.SeoTags) > 0 {
  12. seoTags = RawHTML(input.SeoTags)
  13. }
  14. var canonicalLink HTMLComponent
  15. if len(input.CanonicalLink) > 0 {
  16. canonicalLink = Link(string(input.CanonicalLink)).Attr("ref", "canonical")
  17. }
  18. var structureData HTMLComponent
  19. if len(input.StructuredData) > 0 {
  20. structureData = RawHTML(input.StructuredData)
  21. }
  22. var freeStyleCss HTMLComponent
  23. if len(input.FreeStyleCss) > 0 {
  24. freeStyleCss = Style(strings.Join(input.FreeStyleCss, "\n"))
  25. }
  26. js := "https://the-plant.com/assets/app/container.4f902c4.js"
  27. css := "https://the-plant.com/assets/app/container.4f902c4.css"
  28. domain := "https://example.qor5.theplant-dev.com"
  29. head := Components(
  30. Meta().Attr("charset", "utf-8"),
  31. seoTags,
  32. canonicalLink,
  33. Meta().Attr("http-equiv", "X-UA-Compatible").Content("IE=edge"),
  34. Meta().Content("true").Name("HandheldFriendly"),
  35. Meta().Content("yes").Name("apple-mobile-web-app-capable"),
  36. Meta().Content("black").Name("apple-mobile-web-app-status-bar-style"),
  37. Meta().Name("format-detection").Content("telephone=no"),
  38. Meta().Name("viewport").Content("width=device-width, initial-scale=1"),
  39. Link("").Rel("stylesheet").Type("text/css").Href(css),
  40. If(len(input.EditorCss) > 0, input.EditorCss...),
  41. freeStyleCss,
  42. //RawHTML(dataLayer),
  43. structureData,
  44. scriptWithCodes(input.FreeStyleTopJs),
  45. )
  46. ctx.Injector.HTMLLang(input.Page.LocaleCode)
  47. ctx.Injector.HeadHTML(MustString(head, nil))
  48. return Body(
  49. //It's required as the body first element!
  50. If(input.Header != nil, input.Header),
  51. body,
  52. If(input.Footer != nil, input.Footer),
  53. Script("").Src(js),
  54. scriptWithCodes(input.FreeStyleBottomJs),
  55. ).Attr("data-site-domain", domain)
  56. }
  57. func scriptWithCodes(jscodes []string) HTMLComponent {
  58. var js HTMLComponent
  59. if len(jscodes) > 0 {
  60. js = Script(fmt.Sprintf(`
  61. try {
  62. %s
  63. } catch (error) {
  64. console.log(error);
  65. }
  66. `, strings.Join(jscodes, "\n")))
  67. }
  68. return js
  69. }