Common isomorphic functionality in one kit.

templateset.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. "encoding/gob"
  9. "errors"
  10. "html/template"
  11. "io/ioutil"
  12. "log"
  13. "os"
  14. "path/filepath"
  15. "strings"
  16. )
  17. var CogStaticAssetsSearchPaths []string
  18. type TemplateSet struct {
  19. members map[string]*Template
  20. Funcs template.FuncMap
  21. bundle *TemplateBundle
  22. TemplateFilesPath string
  23. }
  24. func NewTemplateSet() *TemplateSet {
  25. return &TemplateSet{
  26. members: map[string]*Template{},
  27. Funcs: template.FuncMap{},
  28. }
  29. }
  30. func (t *TemplateSet) Members() map[string]*Template {
  31. return t.members
  32. }
  33. func (t *TemplateSet) Bundle() *TemplateBundle {
  34. return t.bundle
  35. }
  36. func (t *TemplateSet) AddTemplateFile(name, filename string, templateType int8) error {
  37. contents, err := ioutil.ReadFile(filename)
  38. if err != nil {
  39. return err
  40. }
  41. tpl, err := template.New(name).Funcs(t.Funcs).Parse(string(contents))
  42. template := Template{
  43. Template: tpl,
  44. templateType: templateType,
  45. }
  46. t.members[tpl.Name()] = &template
  47. return nil
  48. }
  49. func (t *TemplateSet) MakeAllAssociations() error {
  50. for _, template := range t.members {
  51. for _, member := range t.members {
  52. if member.Lookup(template.NameWithPrefix()) == nil {
  53. if _, err := member.AddParseTree(template.NameWithPrefix(), template.Tree); err != nil {
  54. println(err)
  55. return err
  56. }
  57. }
  58. }
  59. }
  60. return nil
  61. }
  62. func (t *TemplateSet) ImportTemplatesFromMap(templateMap map[string]string) error {
  63. for name, templateContents := range templateMap {
  64. var templateType int8
  65. if strings.Contains(name, PrefixNamePartial) {
  66. templateType = TemplatePartial
  67. } else if strings.Contains(name, PrefixNameLayout) {
  68. templateType = TemplateLayout
  69. } else {
  70. templateType = TemplateRegular
  71. }
  72. tpl, err := template.New(name).Funcs(t.Funcs).Parse(templateContents)
  73. if err != nil {
  74. log.Println("Encountered error when attempting to parse template: ", err)
  75. return err
  76. }
  77. tmpl := Template{
  78. Template: tpl,
  79. templateType: templateType,
  80. }
  81. t.members[name] = &tmpl
  82. }
  83. t.MakeAllAssociations()
  84. return nil
  85. }
  86. func (t *TemplateSet) Render(templateName string, params *RenderParams) {
  87. t.Members()[templateName].Render(params)
  88. }
  89. func (t *TemplateSet) PersistTemplateBundleToDisk() error {
  90. dirPath := filepath.Dir(StaticTemplateBundleFilePath)
  91. if _, err := os.Stat(dirPath); os.IsNotExist(err) {
  92. return errors.New("The specified directory for the StaticTemplateBundleFilePath, " + dirPath + ", does not exist!")
  93. } else {
  94. var templateContentItemsBuffer bytes.Buffer
  95. enc := gob.NewEncoder(&templateContentItemsBuffer)
  96. m := t.bundle.Items()
  97. err := enc.Encode(&m)
  98. if err != nil {
  99. return err
  100. }
  101. err = ioutil.WriteFile(StaticTemplateBundleFilePath, templateContentItemsBuffer.Bytes(), 0644)
  102. if err != nil {
  103. return err
  104. } else {
  105. return nil
  106. }
  107. }
  108. }
  109. func (t *TemplateSet) RestoreTemplateBundleFromDisk() error {
  110. if _, err := os.Stat(StaticTemplateBundleFilePath); os.IsNotExist(err) {
  111. return errors.New("The StaticTemplateBundleFilePath, " + StaticTemplateBundleFilePath + ", does not exist")
  112. } else {
  113. data, err := ioutil.ReadFile(StaticTemplateBundleFilePath)
  114. if err != nil {
  115. return err
  116. }
  117. var templateBundleMap map[string]string
  118. var templateBundleMapBuffer bytes.Buffer
  119. dec := gob.NewDecoder(&templateBundleMapBuffer)
  120. templateBundleMapBuffer = *bytes.NewBuffer(data)
  121. err = dec.Decode(&templateBundleMap)
  122. if err != nil {
  123. return err
  124. }
  125. t.ImportTemplatesFromMap(templateBundleMap)
  126. bundle := &TemplateBundle{items: templateBundleMap}
  127. t.bundle = bundle
  128. return nil
  129. }
  130. }
  131. func (t *TemplateSet) GatherTemplates() {
  132. if UseStaticTemplateBundleFile == true {
  133. err := t.RestoreTemplateBundleFromDisk()
  134. if err != nil {
  135. log.Println("Didn't find a template bundle from disk, will generate a new template bundle.")
  136. } else {
  137. return
  138. }
  139. }
  140. bundle := NewTemplateBundle()
  141. templatesPath := t.TemplateFilesPath
  142. if templatesPath == "" {
  143. templatesPath = TemplateFilesPath
  144. }
  145. bundle.importTemplateFileContents(templatesPath)
  146. t.ImportTemplatesFromMap(bundle.Items())
  147. t.bundle = bundle
  148. if StaticTemplateBundleFilePath != "" {
  149. err := t.PersistTemplateBundleToDisk()
  150. if err != nil {
  151. log.Println("Failed to persist the template bundle to disk, in GatherTemplates, with error: ", err)
  152. }
  153. }
  154. }
  155. func (t *TemplateSet) GatherCogTemplates(cogTemplatePath string, prefixName string, templateFileExtension string) {
  156. if ShouldBundleStaticAssets == false || UseStaticTemplateBundleFile == true {
  157. return
  158. }
  159. bundle := NewTemplateBundle()
  160. templatesPath := cogTemplatePath
  161. bundle.importTemplateFileContentsForCog(templatesPath, prefixName, templateFileExtension)
  162. t.ImportTemplatesFromMap(bundle.Items())
  163. for k, v := range bundle.Items() {
  164. t.bundle.items[k] = v
  165. }
  166. if StaticTemplateBundleFilePath != "" {
  167. err := t.PersistTemplateBundleToDisk()
  168. if err != nil {
  169. log.Println("Failed to persist the template bundle to disk, in GatherCogTemplates, with error: ", err)
  170. }
  171. }
  172. }
  173. func StaticTemplateBundleFileExists() bool {
  174. if _, err := os.Stat(StaticTemplateBundleFilePath); os.IsNotExist(err) {
  175. return false
  176. } else {
  177. return true
  178. }
  179. }
  180. func RegisterStaticAssetsSearchPath(path string) {
  181. //fmt.Println("cog search path: ", path)
  182. CogStaticAssetsSearchPaths = append(CogStaticAssetsSearchPaths, path)
  183. }