template.go
func (tmpl *Template) Render(templateName string, obj interface{}, request *http.Request, writer http.ResponseWriter) (template.HTML, error) {
	var (
		content []byte
		t       *template.Template
		err     error
		funcMap = tmpl.funcMapMaker(request, writer)
		render  = func(name string, objs ...interface{}) (template.HTML, error) {
			var (
				err           error
				renderObj     interface{}
				renderContent []byte
			)

			if len(objs) == 0 {
				// default obj
				renderObj = obj
			} else {
				// overwrite obj
				for _, o := range objs {
					renderObj = o
					break
				}
			}

			if renderContent, err = tmpl.findTemplate(name); err == nil {
				var partialTemplate *template.Template
				result := bytes.NewBufferString("")
				if partialTemplate, err = template.New(filepath.Base(name)).Funcs(funcMap).Parse(string(renderContent)); err == nil {
					if err = partialTemplate.Execute(result, renderObj); err == nil {
						return template.HTML(result.String()), err
					}
				}
			} else {
				err = fmt.Errorf("failed to find template: %v", name)
			}

			if err != nil {
				fmt.Println(err)
			}

			return "", err
		}
	)

	// funcMaps
	funcMap["render"] = render
	funcMap["yield"] = func() (template.HTML, error) { return render(templateName) }

	layout := tmpl.layout
	usingDefaultLayout := false

	if layout == "" && tmpl.usingDefaultLayout {
		usingDefaultLayout = true
		layout = tmpl.render.DefaultLayout
	}

	if layout != "" {
		content, err = tmpl.findTemplate(filepath.Join("layouts", layout))
		if err == nil {
			if t, err = template.New("").Funcs(funcMap).Parse(string(content)); err == nil {
				var tpl bytes.Buffer
				if err = t.Execute(&tpl, obj); err == nil {
					return template.HTML(tpl.String()), nil
				}
			}
		} else if !usingDefaultLayout {
			err = fmt.Errorf("Failed to render layout: '%v.tmpl', got error: %v", filepath.Join("layouts", tmpl.layout), err)
			fmt.Println(err)
			return template.HTML(""), err
		}
	}

	if content, err = tmpl.findTemplate(templateName); err == nil {
		if t, err = template.New("").Funcs(funcMap).Parse(string(content)); err == nil {
			var tpl bytes.Buffer
			if err = t.Execute(&tpl, obj); err == nil {
				return template.HTML(tpl.String()), nil
			}
		}
	} else {
		err = fmt.Errorf("failed to find template: %v", templateName)
	}

	if err != nil {
		fmt.Println(err)
	}
	return template.HTML(""), err
}
func (tmpl *Template) Render(templateName string, obj interface{}, request *http.Request, writer http.ResponseWriter) (template.HTML, error) {
	var (
		content []byte
		t       *template.Template
		err     error
		funcMap = tmpl.funcMapMaker(request, writer)
		render  = func(name string, objs ...interface{}) (template.HTML, error) {
			var (
				err           error
				renderObj     interface{}
				renderContent []byte
			)

			if len(objs) == 0 {
				// default obj
				renderObj = obj
			} else {
				// overwrite obj
				for _, o := range objs {
					renderObj = o
					break
				}
			}

			if renderContent, err = tmpl.findTemplate(name); err == nil {
				var partialTemplate *template.Template
				result := bytes.NewBufferString("")
				if partialTemplate, err = template.New(filepath.Base(name)).Funcs(funcMap).Parse(string(renderContent)); err == nil {
					if err = partialTemplate.Execute(result, renderObj); err == nil {
						return template.HTML(result.String()), err
					}
				}
			} else {
				err = fmt.Errorf("failed to find template: %v", name)
			}

			if err != nil {
				fmt.Println(err)
			}

			return "", err
		}
	)

	// funcMaps
	funcMap["render"] = render
	funcMap["yield"] = func() (template.HTML, error) { return render(templateName) }

	layout := tmpl.layout
	usingDefaultLayout := false

	if layout == "" && tmpl.usingDefaultLayout {
		usingDefaultLayout = true
		layout = tmpl.render.DefaultLayout
	}

	if layout != "" {
		content, err = tmpl.findTemplate(filepath.Join("layouts", layout))
		if err == nil {
			if t, err = template.New("").Funcs(funcMap).Parse(string(content)); err == nil {
				var tpl bytes.Buffer
				if err = t.Execute(&tpl, obj); err == nil {
					return template.HTML(tpl.String()), nil
				}
			}
		} else if !usingDefaultLayout {
			err = fmt.Errorf("Failed to render layout: '%v.tmpl', got error: %v", filepath.Join("layouts", tmpl.layout), err)
			fmt.Println(err)
			return template.HTML(""), err
		}
	}

	if content, err = tmpl.findTemplate(templateName); err == nil {
		if t, err = template.New("").Funcs(funcMap).Parse(string(content)); err == nil {
			var tpl bytes.Buffer
			if err = t.Execute(&tpl, obj); err == nil {
				return template.HTML(tpl.String()), nil
			}
		}
	} else {
		err = fmt.Errorf("failed to find template: %v", templateName)
	}

	if err != nil {
		fmt.Println(err)
	}
	return template.HTML(""), err
}
// Render render tmpl