Common isomorphic functionality in one kit.

template.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. StaticTemplateBundleFilePath = ""
  27. )
  28. type Template struct {
  29. *template.Template
  30. templateType int8
  31. }
  32. const (
  33. PlacementAppendTo = iota
  34. PlacementReplaceInnerContents
  35. PlacementInsertBefore
  36. )
  37. type RenderParams struct {
  38. Data interface{}
  39. Writer io.Writer
  40. Element dom.Element
  41. Disposition int8
  42. Attributes map[string]string
  43. ShouldPopulateRenderedContent bool
  44. RenderedContent string
  45. ShouldSkipFinalRenderStep bool
  46. PageTitle string
  47. }
  48. func (t *Template) GetTemplateType() int8 {
  49. if t == nil {
  50. return -1
  51. } else {
  52. return t.templateType
  53. }
  54. }
  55. func (t *Template) NameWithPrefix() string {
  56. var prefixName string
  57. switch t.templateType {
  58. case TemplateRegular:
  59. prefixName = ""
  60. case TemplatePartial:
  61. prefixName = PrefixNamePartial
  62. case TemplateLayout:
  63. prefixName = PrefixNameLayout
  64. }
  65. if strings.HasPrefix(t.Name(), prefixName) {
  66. return t.Name()
  67. } else {
  68. return prefixName + t.Name()
  69. }
  70. }
  71. func (t *Template) Render(params *RenderParams) error {
  72. if OperatingEnvironment() == ServerEnvironment && (params.Writer == nil) {
  73. return errors.New("Either the response writer and/or the request is nil!")
  74. }
  75. if OperatingEnvironment() == WebBrowserEnvironment && params.Element == nil {
  76. return errors.New("The element to render relative to is nil!")
  77. }
  78. switch OperatingEnvironment() {
  79. case WebBrowserEnvironment:
  80. t.RenderTemplateOnClient(params)
  81. case ServerEnvironment:
  82. t.RenderTemplateOnServer(params)
  83. }
  84. return nil
  85. }
  86. func (t *Template) RenderTemplateOnClient(params *RenderParams) {
  87. var tpl bytes.Buffer
  88. if err := t.Execute(&tpl, params.Data); err != nil {
  89. log.Println("Error encountered when attempting to render template on client: ", err)
  90. }
  91. if params.ShouldPopulateRenderedContent == true {
  92. params.RenderedContent = string(tpl.Bytes())
  93. }
  94. if params.ShouldSkipFinalRenderStep == true {
  95. return
  96. }
  97. div := dom.GetWindow().Document().CreateElement("div").(*dom.HTMLDivElement)
  98. div.SetInnerHTML(string(tpl.Bytes()))
  99. if _, ok := params.Attributes["id"]; ok {
  100. div.SetID(params.Attributes["id"])
  101. }
  102. if _, ok := params.Attributes["class"]; ok {
  103. div.SetAttribute("class", params.Attributes["class"])
  104. }
  105. switch params.Disposition {
  106. case PlacementAppendTo:
  107. params.Element.AppendChild(div)
  108. case PlacementReplaceInnerContents:
  109. params.Element.SetInnerHTML(div.OuterHTML())
  110. case PlacementInsertBefore:
  111. params.Element.ParentNode().InsertBefore(div, params.Element)
  112. default:
  113. params.Element.AppendChild(div)
  114. }
  115. if params.PageTitle != "" && params.ShouldPopulateRenderedContent == false {
  116. dom.GetWindow().Document().Underlying().Set("title", params.PageTitle)
  117. }
  118. }
  119. func (t *Template) RenderTemplateOnServer(params *RenderParams) {
  120. w := params.Writer
  121. var tpl bytes.Buffer
  122. if err := t.Execute(&tpl, params.Data); err != nil {
  123. log.Println("Error encountered when attempting to render template on server: ", err)
  124. }
  125. w.Write(tpl.Bytes())
  126. }