|
| 1 | +package slim |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | + |
| 13 | + "github.com/gofiber/template/utils" |
| 14 | + "github.com/mattn/go-slim" |
| 15 | + "github.com/valyala/bytebufferpool" |
| 16 | +) |
| 17 | + |
| 18 | +// Engine struct |
| 19 | +type Engine struct { |
| 20 | + // delimiters |
| 21 | + left string |
| 22 | + right string |
| 23 | + // views folder |
| 24 | + directory string |
| 25 | + // http.FileSystem supports embedded files |
| 26 | + fileSystem http.FileSystem |
| 27 | + // views extension |
| 28 | + extension string |
| 29 | + // layout variable name that incapsulates the template |
| 30 | + layout string |
| 31 | + // reload on each render |
| 32 | + reload bool |
| 33 | + // debug prints the parsed templates |
| 34 | + debug bool |
| 35 | + // lock for funcmap and templates |
| 36 | + mutex sync.RWMutex |
| 37 | + // template funcmap |
| 38 | + funcmap map[string]interface{} |
| 39 | + // templates |
| 40 | + Templates map[string]*slim.Template |
| 41 | +} |
| 42 | + |
| 43 | +// New returns a Handlebar render engine for Fiber |
| 44 | +func New(directory, extension string) *Engine { |
| 45 | + engine := &Engine{ |
| 46 | + left: "{{", |
| 47 | + right: "}}", |
| 48 | + directory: directory, |
| 49 | + extension: extension, |
| 50 | + layout: "embed", |
| 51 | + funcmap: make(map[string]interface{}), |
| 52 | + } |
| 53 | + return engine |
| 54 | +} |
| 55 | + |
| 56 | +func NewFileSystem(fs http.FileSystem, extension string) *Engine { |
| 57 | + engine := &Engine{ |
| 58 | + left: "{{", |
| 59 | + right: "}}", |
| 60 | + directory: "/", |
| 61 | + fileSystem: fs, |
| 62 | + extension: extension, |
| 63 | + layout: "embed", |
| 64 | + funcmap: make(map[string]interface{}), |
| 65 | + } |
| 66 | + return engine |
| 67 | +} |
| 68 | + |
| 69 | +// Layout defines the variable name that will incapsulate the template |
| 70 | +func (e *Engine) Layout(key string) *Engine { |
| 71 | + e.layout = key |
| 72 | + return e |
| 73 | +} |
| 74 | + |
| 75 | +// Delims sets the action delimiters to the specified strings, to be used in |
| 76 | +// templates. An empty delimiter stands for the |
| 77 | +// corresponding default: {{ or }}. |
| 78 | +func (e *Engine) Delims(left, right string) *Engine { |
| 79 | + fmt.Println("delims: this method is not supported for django") |
| 80 | + return e |
| 81 | +} |
| 82 | + |
| 83 | +// AddFunc adds the function to the template's function map. |
| 84 | +// It is legal to overwrite elements of the default actions |
| 85 | +func (e *Engine) AddFunc(name string, fn interface{}) *Engine { |
| 86 | + e.mutex.Lock() |
| 87 | + e.funcmap[name] = fn |
| 88 | + e.mutex.Unlock() |
| 89 | + return e |
| 90 | +} |
| 91 | + |
| 92 | +// Reload if set to true the templates are reloading on each render, |
| 93 | +// use it when you're in development and you don't want to restart |
| 94 | +// the application when you edit a template file. |
| 95 | +func (e *Engine) Reload(enabled bool) *Engine { |
| 96 | + e.reload = enabled |
| 97 | + return e |
| 98 | +} |
| 99 | + |
| 100 | +// Debug will print the parsed templates when Load is triggered. |
| 101 | +func (e *Engine) Debug(enabled bool) *Engine { |
| 102 | + e.debug = enabled |
| 103 | + return e |
| 104 | +} |
| 105 | + |
| 106 | +// Parse is deprecated, please use Load() instead |
| 107 | +func (e *Engine) Parse() error { |
| 108 | + fmt.Println("Parse() is deprecated, please use Load() instead.") |
| 109 | + return e.Load() |
| 110 | +} |
| 111 | + |
| 112 | +// Load parses the templates to the engine. |
| 113 | +func (e *Engine) Load() error { |
| 114 | + // race safe |
| 115 | + e.mutex.Lock() |
| 116 | + defer e.mutex.Unlock() |
| 117 | + |
| 118 | + e.Templates = make(map[string]*slim.Template) |
| 119 | + |
| 120 | + // Loop trough each directory and register template files |
| 121 | + walkFn := func(path string, info os.FileInfo, err error) error { |
| 122 | + // Return error if exist |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + // Skip file if it's a directory or has no file info |
| 127 | + if info == nil || info.IsDir() { |
| 128 | + return nil |
| 129 | + } |
| 130 | + // Get file extension of file |
| 131 | + ext := filepath.Ext(path) |
| 132 | + // Skip file if it does not equal the given template extension |
| 133 | + if ext != e.extension { |
| 134 | + return nil |
| 135 | + } |
| 136 | + // Get the relative file path |
| 137 | + // ./views/html/index.tmpl -> index.tmpl |
| 138 | + rel, err := filepath.Rel(e.directory, path) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + // Reverse slashes '\' -> '/' and |
| 143 | + // partials\footer.tmpl -> partials/footer.tmpl |
| 144 | + name := filepath.ToSlash(rel) |
| 145 | + // Remove ext from name 'index.tmpl' -> 'index' |
| 146 | + name = strings.Replace(name, e.extension, "", -1) |
| 147 | + // Read the file |
| 148 | + // #gosec G304 |
| 149 | + buf, err := utils.ReadFile(path, e.fileSystem) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + // Create new template associated with the current one |
| 154 | + tmpl, err := slim.Parse(bytes.NewReader(buf)) |
| 155 | + if err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + // tmpl.FuncMap(e.funcmap) |
| 159 | + e.Templates[name] = tmpl |
| 160 | + // Debugging |
| 161 | + if e.debug { |
| 162 | + fmt.Printf("views: parsed template: %s\n", name) |
| 163 | + } |
| 164 | + return err |
| 165 | + } |
| 166 | + if e.fileSystem != nil { |
| 167 | + return utils.Walk(e.fileSystem, e.directory, walkFn) |
| 168 | + } |
| 169 | + return filepath.Walk(e.directory, walkFn) |
| 170 | +} |
| 171 | + |
| 172 | +// Execute will render the template by name |
| 173 | +func (e *Engine) Render(out io.Writer, template string, binding interface{}, layout ...string) error { |
| 174 | + if e.reload { |
| 175 | + if err := e.Load(); err != nil { |
| 176 | + return err |
| 177 | + } |
| 178 | + } |
| 179 | + tmpl := e.Templates[template] |
| 180 | + if tmpl == nil { |
| 181 | + return fmt.Errorf("render: template %s does not exist", template) |
| 182 | + } |
| 183 | + if len(layout) > 0 { |
| 184 | + buf := bytebufferpool.Get() |
| 185 | + defer bytebufferpool.Put(buf) |
| 186 | + if err := tmpl.Execute(buf, binding); err != nil { |
| 187 | + return err |
| 188 | + } |
| 189 | + var bind map[string]interface{} |
| 190 | + if bind == nil { |
| 191 | + bind = make(map[string]interface{}, 1) |
| 192 | + } else if context, ok := binding.(map[string]interface{}); ok { |
| 193 | + bind = context |
| 194 | + } else { |
| 195 | + bind = make(map[string]interface{}, 1) |
| 196 | + } |
| 197 | + bind[e.layout] = buf.String() |
| 198 | + lay := e.Templates[layout[0]] |
| 199 | + if lay == nil { |
| 200 | + return fmt.Errorf("render: layout %s does not exist", layout[0]) |
| 201 | + } |
| 202 | + return lay.Execute(out, bind) |
| 203 | + } |
| 204 | + return tmpl.Execute(out, binding) |
| 205 | +} |
0 commit comments