ExecuteWriter should execute a template by its filename with an optional layout and bindingData.
(w io.Writer, filename string, layout string, bindingData interface{})
| 335 | |
| 336 | // ExecuteWriter should execute a template by its filename with an optional layout and bindingData. |
| 337 | func (s *JetEngine) ExecuteWriter(w io.Writer, filename string, layout string, bindingData interface{}) error { |
| 338 | tmpl, err := s.Set.GetTemplate(filename) |
| 339 | if err != nil { |
| 340 | return err |
| 341 | } |
| 342 | |
| 343 | var vars JetRuntimeVars |
| 344 | |
| 345 | if ctx, ok := w.(*context.Context); ok { |
| 346 | runtimeVars := ctx.Values().Get(JetRuntimeVarsContextKey) |
| 347 | if runtimeVars != nil { |
| 348 | if jetVars, ok := runtimeVars.(JetRuntimeVars); ok { |
| 349 | vars = jetVars |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | if viewContextData := ctx.GetViewData(); len(viewContextData) > 0 { // fix #1876 |
| 354 | if vars == nil { |
| 355 | vars = make(JetRuntimeVars) |
| 356 | } |
| 357 | |
| 358 | for k, v := range viewContextData { |
| 359 | val, ok := v.(reflect.Value) |
| 360 | if !ok { |
| 361 | val = reflect.ValueOf(v) |
| 362 | } |
| 363 | vars[k] = val |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if v := ctx.Values().Get(s.jetDataContextKey); v != nil { |
| 368 | if bindingData == nil { |
| 369 | // if bindingData is nil, try to fill them by context key (a middleware can set data). |
| 370 | bindingData = v |
| 371 | } else if m, ok := bindingData.(context.Map); ok { |
| 372 | // else if bindingData are passed to App/Context.View |
| 373 | // and it's map try to fill with the new values passed from a middleware. |
| 374 | if mv, ok := v.(context.Map); ok { |
| 375 | for key, value := range mv { |
| 376 | m[key] = value |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | } |
| 383 | |
| 384 | if bindingData == nil { |
| 385 | return tmpl.Execute(w, vars, nil) |
| 386 | } |
| 387 | |
| 388 | if vars == nil { |
| 389 | vars = make(JetRuntimeVars) |
| 390 | } |
| 391 | |
| 392 | /* fixed on jet v4.0.0, so no need of this: |
| 393 | if m, ok := bindingData.(context.Map); ok { |
| 394 | var jetData interface{} |
nothing calls this directly
no test coverage detected