ExecuteWriter executes a template and writes its result to the w writer.
(w io.Writer, filename string, layout string, bindingData interface{})
| 212 | |
| 213 | // ExecuteWriter executes a template and writes its result to the w writer. |
| 214 | func (s *HandlebarsEngine) ExecuteWriter(w io.Writer, filename string, layout string, bindingData interface{}) error { |
| 215 | // re-parse the templates if reload is enabled. |
| 216 | if s.reload { |
| 217 | if err := s.Load(); err != nil { |
| 218 | return err |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | isLayout := false |
| 223 | layout = getLayout(layout, s.layout) |
| 224 | renderFilename := filename |
| 225 | |
| 226 | if layout != "" { |
| 227 | isLayout = true |
| 228 | renderFilename = layout // the render becomes the layout, and the name is the partial. |
| 229 | } |
| 230 | |
| 231 | if tmpl := s.fromCache(renderFilename); tmpl != nil { |
| 232 | binding := bindingData |
| 233 | if isLayout { |
| 234 | var context map[string]interface{} |
| 235 | if m, is := binding.(map[string]interface{}); is { // handlebars accepts maps, |
| 236 | context = m |
| 237 | } else { |
| 238 | return fmt.Errorf("please provide a map[string]interface{} type as the binding instead of the %#v", binding) |
| 239 | } |
| 240 | |
| 241 | contents, err := s.executeTemplateBuf(filename, binding) |
| 242 | if err != nil { |
| 243 | return err |
| 244 | } |
| 245 | if context == nil { |
| 246 | context = make(map[string]interface{}, 1) |
| 247 | } |
| 248 | // I'm implemented the {{ yield . }} as with the rest of template engines, so this is not inneed for iris, but the user can do that manually if want |
| 249 | // there is no performance cost: raymond.RegisterPartialTemplate(name, tmpl) |
| 250 | context["yield"] = raymond.SafeString(contents) |
| 251 | } |
| 252 | |
| 253 | res, err := tmpl.Exec(binding) |
| 254 | if err != nil { |
| 255 | return err |
| 256 | } |
| 257 | _, err = fmt.Fprint(w, res) |
| 258 | return err |
| 259 | } |
| 260 | |
| 261 | return ErrNotExist{ |
| 262 | Name: fmt.Sprintf("%s (file: %s)", renderFilename, filename), |
| 263 | IsLayout: false, |
| 264 | Data: bindingData, |
| 265 | } |
| 266 | } |