Common isomorphic functionality in one kit.

template.go 3.3KB

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