This is a creation in Article, where the information may have evolved or changed.
Jade is a high-performance HTML template engine that is affected by HAML and is implemented using JavaScript. Jade is also supported on the client side, and its code is much more readable than HTML, and Jade is a more commonly used HTML template.
Beego is a Go-language Web application open-source web framework, while Beego supports more complex template engines from 1.7.0, and of course includes support for Jade, which supports more complex template engine PR addresses Https://github.com/astaxie /beego/pull/1940.
Before introducing the use of jade, take a look at the html/template
package below go.
Html/template
In the Go language, the html/template
package is a very powerful HTML template package, combined text/template
with the template syntax, basically can meet most of the HTML template requirements, whether it is beego in the default support of the two template formats .tpl
and .html
, jade
ace
and Can be parsed into html/template
a template object in use, which means that the HTML template we use is ultimately based on the html/template
package implementation.
html/template
Usage examples:
package mainimport ( "html/template")type User struct { Name string}func main() { t := template.New("template example") t, _ = t.Parse("hello {{.Name}}!") p := User{Name: "jjz"} t.Execute(os.Stdout, p)}
The example above will output a string: hello jjz
.
From the example above, we can see how to create a new template and then use a template function to Parse()
load the template content from a string, using a template function to replace the field with a template Execute()
. Replace the syntax for the template field, where the field is the field {{}}
you want to replace, {{. Name}}
indicating the name of the field you want to replace.
Beego
The way to use complex templates is simply to add a template engine function that iterates through the files in the run of the project views
, parses the specified file into an template.Template
object, and returns the object provided for use, which is the template engine function: beego.AddTemplateEngine
.
Addtemplateengine
beego.AddTemplateEngine
is a function used to convert the specified file into template.Template
an object. Its first parameter is the suffix name of the file, in which the views
file containing the prefix will go to the method for processing. His second argument is a function that handles the conversion of a file and returns an template.Template
object at the end. With this function, we have one less way to parse the jade file template.Template
, fortunately someone has done Jade's go language implementation.
Jade.go
Jade.go is the go language implementation of Jade.
jade.go
Use, first install the Jade.go:
Go get Github.com/joker/jade
jade.go
Examples of Use:
func main() { tpl, err := jade.Parse("name_of_tpl", "doctype 5: html: body: p Hello world!") if err != nil { return } fmt.Printf( "%s", tpl )}
Output string:
<!DOCTYPE html>
jade.go
you can use it in Beego, convert the jade file to a template object, and add a way to jade.go
parse the jade template in Beego:
func addJadeTemplate() { beego.AddTemplateEngine("jade", func(root, path string, funcs template.FuncMap) (*template.Template, error) { jadePath := filepath.Join(root, path) content, err := utils.ReadFile(jadePath) fmt.Println(content) if err != nil { return nil, fmt.Errorf("error loading jade template: %v", err) } tpl, err := jade.Parse("name_of_tpl", content) if err != nil { return nil, fmt.Errorf("error loading jade template: %v", err) } fmt.Println("html:\n%s",tpl) tmp := template.New("Person template") tmp, err = tmp.Parse(tpl) if err != nil { return nil, fmt.Errorf("error loading jade template: %v", err) } fmt.Println(tmp) return tmp, err })}
jade.go
Currently only supports the use of strings to remove the jade template, so you need to first read the contents of the. Jade file as a string. Ways to read files:
func ReadFile(path string) (str string, err error) { fi, err := os.Open(path) defer fi.Close() fd, err := ioutil.ReadAll(fi) str = string(fd) return}
The result of Jade.go parsing is also a string, so you need to convert the string to an object in your code template.Template
, using the Template.Parse()
method.
The engine method that parses the jade template needs to be main()
called in, and after adding the template transformation engine of jade, you can Beego
use the jade template in.
Use of the Jade template
First define a page home.jade
:
doctype htmlhtml head title pageTitle body h1 jade .content {{.content}}
Here {{.content}}
is the field that needs to be replaced, the Controller
layer code:
func (c *MainController) Jade() { c.Data["content"] = "this is jade template" c.TplName = "home.jade"}
Page code generated after the run:
<!DOCTYPE html>
By adding an engine for parsing templates, you can use the Jade template in Beego, Beego support for complex template engines from 1.7.0 onwards, not just for Jade, but also for other template engines.
In addition to Jade, it is recommended to use the Acehtml template engine in this PR.
Ace
Ace is a Go language HTML template engine that draws on slim and Jade and has a high popularity in the go language.
ace模板
In Beego Jade
, a dependent package that is installed first, similar to using ace
:
Go get github.com/yosssi/ace
In the main function, add the ACE template parsing engine:
func addAceTemplate() { beego.AddTemplateEngine("ace", func(root, path string, funcs template.FuncMap) (*template.Template, error) { aceOptions := &ace.Options{DynamicReload: true, FuncMap: funcs} aceBasePath := filepath.Join(root, "base") aceInnerPath := filepath.Join(root, strings.TrimSuffix(path, ".ace")) tpl, err := ace.Load(aceBasePath, aceInnerPath, aceOptions) if err != nil { return nil, fmt.Errorf("error loading ace template: %v", err) } return tpl, nil }) }
Note that you need to specify one using the Ace template base.ace
, and ace.Load()
you can return an object directly by using a function template.Template
, without the need to do any more conversion work.
After adding the template engine, you can add the. ace file directly to the views to create a new home.ace
file:
= doctype htmlhtml lang=en head meta charset=utf-8 title Base and Inner Template body h1 ace .content {{.content}}
The same {. Content}} is also required to replace the layer with the controller
required content, controller layer code:
func (c *MainController)Ace() { c.Data["content"] = "this is ace template" c.TplName = "home.ace"}
Example code address: https://github.com/jjz/go/tree/master/template