Common isomorphic functionality in one kit.

template.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // The Isomorphic Go Project
  2. // Copyright (c) Wirecog, LLC. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license, which can be found in the LICENSE file.
  5. package isokit
  6. import (
  7. "bytes"
  8. "errors"
  9. "html/template"
  10. "io"
  11. "log"
  12. "strings"
  13. "honnef.co/go/js/dom"
  14. )
  15. const (
  16. TemplateRegular = iota
  17. TemplatePartial
  18. TemplateLayout
  19. )
  20. var (
  21. PrefixNamePartial = "partials/"
  22. PrefixNameLayout = "layouts/"
  23. TemplateFileExtension = ".tmpl"
  24. TemplateFilesPath = "./templates"
  25. UseStaticTemplateBundleFile = false
  26. UseTemplateBundleFromBuffer = false
  27. StaticTemplateBundleFilePath = ""
  28. ShouldBundleStaticAssets = true
  29. )
  30. type Template struct {
  31. *template.Template
  32. templateType int8
  33. }
  34. const (
  35. PlacementAppendTo = iota
  36. PlacementReplaceInnerContents
  37. PlacementInsertBefore
  38. )
  39. type RenderParams struct {
  40. Data interface{}
  41. Writer io.Writer
  42. Element dom.Element
  43. Disposition int8
  44. Attributes map[string]string
  45. ShouldPopulateRenderedContent bool
  46. RenderedContent string
  47. ShouldSkipFinalRenderStep bool
  48. PageTitle string
  49. }
  50. func (t *Template) GetTemplateType() int8 {
  51. if t == nil {
  52. return -1
  53. } else {
  54. return t.templateType
  55. }
  56. }
  57. func (t *Template) NameWithPrefix() string {
  58. var prefixName string
  59. switch t.templateType {
  60. case TemplateRegular:
  61. prefixName = ""
  62. case TemplatePartial:
  63. prefixName = PrefixNamePartial
  64. case TemplateLayout:
  65. prefixName = PrefixNameLayout
  66. }
  67. if strings.HasPrefix(t.Name(), prefixName) {
  68. return t.Name()
  69. } else {
  70. return prefixName + t.Name()
  71. }
  72. }
  73. func (t *Template) Render(params *RenderParams) error {
  74. if OperatingEnvironment() == ServerEnvironment && (params.Writer == nil) {
  75. return errors.New("Either the response writer and/or the request is nil!")
  76. }
  77. if OperatingEnvironment() == WebBrowserEnvironment && params.Element == nil {
  78. return errors.New("The element to render relative to is nil!")
  79. }
  80. switch OperatingEnvironment() {
  81. case WebBrowserEnvironment:
  82. t.RenderTemplateOnClient(params)
  83. case ServerEnvironment:
  84. t.RenderTemplateOnServer(params)
  85. }
  86. return nil
  87. }
  88. func (t *Template) RenderTemplateOnClient(params *RenderParams) {
  89. var tpl bytes.Buffer
  90. if err := t.Execute(&tpl, params.Data); err != nil {
  91. log.Println("Error encountered when attempting to render template on client: ", err)
  92. }
  93. if params.ShouldPopulateRenderedContent == true {
  94. params.RenderedContent = string(tpl.Bytes())
  95. }
  96. if params.ShouldSkipFinalRenderStep == true {
  97. return
  98. }
  99. div := dom.GetWindow().Document().CreateElement("div").(*dom.HTMLDivElement)
  100. div.SetInnerHTML(string(tpl.Bytes()))
  101. if _, ok := params.Attributes["id"]; ok {
  102. div.SetID(params.Attributes["id"])
  103. }
  104. if _, ok := params.Attributes["class"]; ok {
  105. div.SetAttribute("class", params.Attributes["class"])
  106. }
  107. switch params.Disposition {
  108. case PlacementAppendTo:
  109. params.Element.AppendChild(div)
  110. case PlacementReplaceInnerContents:
  111. params.Element.SetInnerHTML(div.OuterHTML())
  112. case PlacementInsertBefore:
  113. params.Element.ParentNode().InsertBefore(div, params.Element)
  114. default:
  115. params.Element.AppendChild(div)
  116. }
  117. if params.PageTitle != "" && params.ShouldPopulateRenderedContent == false {
  118. dom.GetWindow().Document().Underlying().Set("title", params.PageTitle)
  119. }
  120. }
  121. func (t *Template) RenderTemplateOnServer(params *RenderParams) {
  122. w := params.Writer
  123. var tpl bytes.Buffer
  124. if err := t.Execute(&tpl, params.Data); err != nil {
  125. log.Println("Error encountered when attempting to render template on server: ", err)
  126. }
  127. w.Write(tpl.Bytes())
  128. }