template_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2014 beego Author. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package beego
  15. import (
  16. "os"
  17. "path/filepath"
  18. "testing"
  19. )
  20. var header = `{{define "header"}}
  21. <h1>Hello, astaxie!</h1>
  22. {{end}}`
  23. var index = `<!DOCTYPE html>
  24. <html>
  25. <head>
  26. <title>beego welcome template</title>
  27. </head>
  28. <body>
  29. {{template "block"}}
  30. {{template "header"}}
  31. {{template "blocks/block.tpl"}}
  32. </body>
  33. </html>
  34. `
  35. var block = `{{define "block"}}
  36. <h1>Hello, blocks!</h1>
  37. {{end}}`
  38. func TestTemplate(t *testing.T) {
  39. dir := "_beeTmp"
  40. files := []string{
  41. "header.tpl",
  42. "index.tpl",
  43. "blocks/block.tpl",
  44. }
  45. if err := os.MkdirAll(dir, 0777); err != nil {
  46. t.Fatal(err)
  47. }
  48. for k, name := range files {
  49. os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777)
  50. if f, err := os.Create(filepath.Join(dir, name)); err != nil {
  51. t.Fatal(err)
  52. } else {
  53. if k == 0 {
  54. f.WriteString(header)
  55. } else if k == 1 {
  56. f.WriteString(index)
  57. } else if k == 2 {
  58. f.WriteString(block)
  59. }
  60. f.Close()
  61. }
  62. }
  63. if err := AddViewPath(dir); err != nil {
  64. t.Fatal(err)
  65. }
  66. beeTemplates := beeViewPathTemplates[dir]
  67. if len(beeTemplates) != 3 {
  68. t.Fatalf("should be 3 but got %v", len(beeTemplates))
  69. }
  70. if err := beeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", nil); err != nil {
  71. t.Fatal(err)
  72. }
  73. for _, name := range files {
  74. os.RemoveAll(filepath.Join(dir, name))
  75. }
  76. os.RemoveAll(dir)
  77. }
  78. var menu = `<div class="menu">
  79. <ul>
  80. <li>menu1</li>
  81. <li>menu2</li>
  82. <li>menu3</li>
  83. </ul>
  84. </div>
  85. `
  86. var user = `<!DOCTYPE html>
  87. <html>
  88. <head>
  89. <title>beego welcome template</title>
  90. </head>
  91. <body>
  92. {{template "../public/menu.tpl"}}
  93. </body>
  94. </html>
  95. `
  96. func TestRelativeTemplate(t *testing.T) {
  97. dir := "_beeTmp"
  98. //Just add dir to known viewPaths
  99. if err := AddViewPath(dir); err != nil {
  100. t.Fatal(err)
  101. }
  102. files := []string{
  103. "easyui/public/menu.tpl",
  104. "easyui/rbac/user.tpl",
  105. }
  106. if err := os.MkdirAll(dir, 0777); err != nil {
  107. t.Fatal(err)
  108. }
  109. for k, name := range files {
  110. os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0777)
  111. if f, err := os.Create(filepath.Join(dir, name)); err != nil {
  112. t.Fatal(err)
  113. } else {
  114. if k == 0 {
  115. f.WriteString(menu)
  116. } else if k == 1 {
  117. f.WriteString(user)
  118. }
  119. f.Close()
  120. }
  121. }
  122. if err := BuildTemplate(dir, files[1]); err != nil {
  123. t.Fatal(err)
  124. }
  125. beeTemplates := beeViewPathTemplates[dir]
  126. if err := beeTemplates["easyui/rbac/user.tpl"].ExecuteTemplate(os.Stdout, "easyui/rbac/user.tpl", nil); err != nil {
  127. t.Fatal(err)
  128. }
  129. for _, name := range files {
  130. os.RemoveAll(filepath.Join(dir, name))
  131. }
  132. os.RemoveAll(dir)
  133. }