improve the template reader function
This commit is contained in:
		
							parent
							
								
									6747c55a81
								
							
						
					
					
						commit
						f5adec31c6
					
				| @ -197,35 +197,9 @@ func (c *Controller) RenderString() (string, error) { | |||||||
| 
 | 
 | ||||||
| // RenderBytes returns the bytes of rendered template string. Do not send out response. | // RenderBytes returns the bytes of rendered template string. Do not send out response. | ||||||
| func (c *Controller) RenderBytes() ([]byte, error) { | func (c *Controller) RenderBytes() ([]byte, error) { | ||||||
| 	//if the controller has set layout, then first get the tplname's content set the content to the layout | 	buf, err := c.renderTemplate() | ||||||
| 	var buf bytes.Buffer | 	//if the controller has set layout, then first get the tplName's content set the content to the layout | ||||||
| 	var err error | 	if err == nil && c.Layout != "" { | ||||||
| 	if c.Layout != "" { |  | ||||||
| 		if c.TplName == "" { |  | ||||||
| 			c.TplName = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt |  | ||||||
| 		} |  | ||||||
| 
 |  | ||||||
| 		if BConfig.RunMode == DEV { |  | ||||||
| 			buildFiles := []string{c.TplName} |  | ||||||
| 			if c.LayoutSections != nil { |  | ||||||
| 				for _, sectionTpl := range c.LayoutSections { |  | ||||||
| 					if sectionTpl == "" { |  | ||||||
| 						continue |  | ||||||
| 					} |  | ||||||
| 					buildFiles = append(buildFiles, sectionTpl) |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 			BuildTemplate(BConfig.WebConfig.ViewsPath, buildFiles...) |  | ||||||
| 		} |  | ||||||
| 		if t := getTemplateByName(c.TplName); t == nil { |  | ||||||
| 			panic("can't find templatefile in the path:" + c.TplName) |  | ||||||
| 		} else { |  | ||||||
| 			err = t.ExecuteTemplate(&buf, c.TplName, c.Data) |  | ||||||
| 			if err != nil { |  | ||||||
| 				Trace("template Execute err:", err) |  | ||||||
| 				return nil, err |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 		c.Data["LayoutContent"] = template.HTML(buf.String()) | 		c.Data["LayoutContent"] = template.HTML(buf.String()) | ||||||
| 
 | 
 | ||||||
| 		if c.LayoutSections != nil { | 		if c.LayoutSections != nil { | ||||||
| @ -234,11 +208,9 @@ func (c *Controller) RenderBytes() ([]byte, error) { | |||||||
| 					c.Data[sectionName] = "" | 					c.Data[sectionName] = "" | ||||||
| 					continue | 					continue | ||||||
| 				} | 				} | ||||||
| 
 |  | ||||||
| 				buf.Reset() | 				buf.Reset() | ||||||
| 				err = getTemplateByName(sectionTpl).ExecuteTemplate(&buf, sectionTpl, c.Data) | 				err = executeTemplate(&buf, sectionTpl, c.Data) | ||||||
| 				if err != nil { | 				if err != nil { | ||||||
| 					Trace("template Execute err:", err) |  | ||||||
| 					return nil, err | 					return nil, err | ||||||
| 				} | 				} | ||||||
| 				c.Data[sectionName] = template.HTML(buf.String()) | 				c.Data[sectionName] = template.HTML(buf.String()) | ||||||
| @ -246,31 +218,29 @@ func (c *Controller) RenderBytes() ([]byte, error) { | |||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		buf.Reset() | 		buf.Reset() | ||||||
| 		err = getTemplateByName(c.Layout).ExecuteTemplate(&buf, c.Layout, c.Data) | 		executeTemplate(&buf, c.Layout, c.Data) | ||||||
| 		if err != nil { |  | ||||||
| 			Trace("template Execute err:", err) |  | ||||||
| 			return nil, err |  | ||||||
| 		} |  | ||||||
| 		return buf.Bytes(), nil |  | ||||||
| 	} | 	} | ||||||
|  | 	return buf.Bytes(), err | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
|  | func (c *Controller) renderTemplate() (bytes.Buffer, error) { | ||||||
|  | 	var buf bytes.Buffer | ||||||
| 	if c.TplName == "" { | 	if c.TplName == "" { | ||||||
| 		c.TplName = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt | 		c.TplName = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt | ||||||
| 	} | 	} | ||||||
| 	if BConfig.RunMode == DEV { | 	if BConfig.RunMode == DEV { | ||||||
| 		BuildTemplate(BConfig.WebConfig.ViewsPath, c.TplName) | 		buildFiles := []string{c.TplName} | ||||||
| 	} | 		if c.Layout != "" && c.LayoutSections != nil { | ||||||
| 	if t := getTemplateByName(c.TplName); t == nil { | 			for _, sectionTpl := range c.LayoutSections { | ||||||
| 		panic("can't find templatefile in the path:" + c.TplName) | 				if sectionTpl == "" { | ||||||
| 	} else { | 					continue | ||||||
| 		buf.Reset() | 				} | ||||||
| 		err = t.ExecuteTemplate(&buf, c.TplName, c.Data) | 				buildFiles = append(buildFiles, sectionTpl) | ||||||
| 		if err != nil { | 			} | ||||||
| 			Trace("template Execute err:", err) |  | ||||||
| 			return nil, err |  | ||||||
| 		} | 		} | ||||||
|  | 		BuildTemplate(BConfig.WebConfig.ViewsPath, buildFiles...) | ||||||
| 	} | 	} | ||||||
| 	return buf.Bytes(), nil | 	return buf, executeTemplate(&buf, c.TplName, c.Data) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // Redirect sends the redirection response to url with status code. | // Redirect sends the redirection response to url with status code. | ||||||
|  | |||||||
							
								
								
									
										14
									
								
								template.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								template.go
									
									
									
									
									
								
							| @ -18,6 +18,7 @@ import ( | |||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"html/template" | 	"html/template" | ||||||
|  | 	"io" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"os" | 	"os" | ||||||
| 	"path/filepath" | 	"path/filepath" | ||||||
| @ -37,12 +38,19 @@ var ( | |||||||
| 	beeTemplateExt = []string{"tpl", "html"} | 	beeTemplateExt = []string{"tpl", "html"} | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func getTemplateByName(name string) *template.Template { | func executeTemplate(wr io.Writer, name string, data interface{}) error { | ||||||
| 	if BConfig.RunMode == "dev" { | 	if BConfig.RunMode == DEV { | ||||||
| 		templatesLock.RLock() | 		templatesLock.RLock() | ||||||
| 		defer templatesLock.RUnlock() | 		defer templatesLock.RUnlock() | ||||||
| 	} | 	} | ||||||
| 	return beeTemplates[name] | 	if t, ok := beeTemplates[name]; ok { | ||||||
|  | 		err := t.ExecuteTemplate(wr, name, data) | ||||||
|  | 		if err != nil { | ||||||
|  | 			Trace("template Execute err:", err) | ||||||
|  | 		} | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	panic("can't find templatefile in the path:" + name) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func init() { | func init() { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user