blocks.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package view
  2. import (
  3. "html/template"
  4. "io"
  5. "github.com/kataras/blocks"
  6. )
  7. // BlocksEngine is an Iris view engine adapter for the blocks view engine.
  8. // The blocks engine is based on the html/template standard Go package.
  9. //
  10. // To initialize a fresh one use the `Blocks` function.
  11. // To wrap an existing one use the `WrapBlocks` function.
  12. //
  13. // It contains the following four default template functions:
  14. // - url "routename" parameters...
  15. // - urlpath "routename" parameters...
  16. // - tr "language" "key" arguments...
  17. // - partial "template_name" data
  18. //
  19. // Read more at: https://github.com/kataras/blocks.
  20. type BlocksEngine struct {
  21. Engine *blocks.Blocks
  22. }
  23. var (
  24. _ Engine = (*BlocksEngine)(nil)
  25. _ EngineFuncer = (*BlocksEngine)(nil)
  26. )
  27. // WrapBlocks wraps an initialized blocks engine and returns its Iris adapter.
  28. // See `Blocks` package-level function too.
  29. func WrapBlocks(v *blocks.Blocks) *BlocksEngine {
  30. return &BlocksEngine{Engine: v}
  31. }
  32. // Blocks returns a new blocks view engine.
  33. // The given "extension" MUST begin with a dot.
  34. //
  35. // See `WrapBlocks` package-level function too.
  36. func Blocks(directory, extension string) *BlocksEngine {
  37. return WrapBlocks(blocks.New(directory).Extension(extension))
  38. }
  39. // Ext returns empty ext as this template engine
  40. // supports template blocks without file suffix.
  41. // Note that, if more than one view engine is registered to a single
  42. // Iris application then, this Blocks engine should be the last entry one.
  43. func (s *BlocksEngine) Ext() string {
  44. return ""
  45. }
  46. // AddFunc implements the `EngineFuncer` which is being used
  47. // by the framework to add template functions like:
  48. // - url func(routeName string, args ...string) string
  49. // - urlpath func(routeName string, args ...string) string
  50. // - tr func(lang, key string, args ...interface{}) string
  51. func (s *BlocksEngine) AddFunc(funcName string, funcBody interface{}) {
  52. s.Engine.Funcs(template.FuncMap{funcName: funcBody})
  53. }
  54. // AddLayoutFunc adds a template function for templates that are marked as layouts.
  55. func (s *BlocksEngine) AddLayoutFunc(funcName string, funcBody interface{}) *BlocksEngine {
  56. s.Engine.LayoutFuncs(template.FuncMap{funcName: funcBody})
  57. return s
  58. }
  59. // Binary sets the function which reads contents based on a filename
  60. // and a function that returns all the filenames.
  61. // These functions are used to parse the corresponding files into templates.
  62. // You do not need to set them when the given "rootDir" was a system directory.
  63. // It's mostly useful when the application contains embedded template files,
  64. // e.g. pass go-bindata's `Asset` and `AssetNames` functions
  65. // to load templates from go-bindata generated content.
  66. func (s *BlocksEngine) Binary(asset blocks.AssetFunc, assetNames blocks.AssetNamesFunc) *BlocksEngine {
  67. s.Engine.Assets(asset, assetNames)
  68. return s
  69. }
  70. // Layout sets the default layout which inside should use
  71. // the {{ template "content" . }} to render the main template.
  72. //
  73. // Example for ./views/layouts/main.html:
  74. // Blocks("./views", ".html").Layout("layouts/main")
  75. func (s *BlocksEngine) Layout(layoutName string) *BlocksEngine {
  76. s.Engine.DefaultLayout(layoutName)
  77. return s
  78. }
  79. // Reload if called with a true parameter,
  80. // each `ExecuteWriter` call will re-parse the templates.
  81. // Useful when the application is at a development stage.
  82. func (s *BlocksEngine) Reload(b bool) *BlocksEngine {
  83. s.Engine.Reload(b)
  84. return s
  85. }
  86. // Load parses the files into templates.
  87. func (s *BlocksEngine) Load() error {
  88. return s.Engine.Load()
  89. }
  90. // ExecuteWriter renders a template on "w".
  91. func (s *BlocksEngine) ExecuteWriter(w io.Writer, tmplName, layoutName string, data interface{}) error {
  92. if layoutName == NoLayout {
  93. layoutName = ""
  94. }
  95. return s.Engine.ExecuteTemplate(w, tmplName, layoutName, data)
  96. }