<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">(window.webpackJsonp=window.webpackJsonp||[]).push([[199],{520:function(t,e,a){"use strict";a.r(e);var s=a(14),n=Object(s.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"template-engine"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#template-engine"}},[t._v("#")]),t._v(" Template Engine")]),t._v(" "),e("p",[t._v("Beego uses Go's built-in package "),e("code",[t._v("html/template")]),t._v(" as the template parser.  Upon startup, it will compile and cache the templates into a map for efficient rendering.")]),t._v(" "),e("h2",{attrs:{id:"template-directory"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#template-directory"}},[t._v("#")]),t._v(" Template Directory")]),t._v(" "),e("p",[t._v("The default template directory for Beego is "),e("code",[t._v("views")]),t._v(". Template files can be put into this directory and Beego will parse and cache them automatically. However, if the development mode is enabled, Beego parses templates every time without caching. Beego can only have one template directory which can be customized:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('web.BConfig.WebConfig.ViewsPath = "myviewpath"\n')])])]),e("p",[t._v("You can add alternative template directories by calling")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('web.AddViewPath("moreViews")\n')])])]),e("p",[t._v("This will parse and cache template files in this directory and allow you to use them by setting ViewPath on a Controller:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('this.ViewPath = "moreViews"\n')])])]),e("p",[t._v('Setting a ViewPath to a directory which was not previously registered with AddViewPath() will fail with "Unknown view path"')]),t._v(" "),e("h2",{attrs:{id:"auto-rendering"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#auto-rendering"}},[t._v("#")]),t._v(" Auto Rendering")]),t._v(" "),e("p",[t._v("You don't need to render and output templates manually. Beego will call Render automatically after finishing the method. You can disable auto rendering in the configuration file or in "),e("code",[t._v("main.go")]),t._v(" if you don't need it.")]),t._v(" "),e("p",[t._v("In configuration file:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("autorender = false\n")])])]),e("p",[t._v("In main.go:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("web.BConfig.WebConfig.AutoRender = false\n")])])]),e("h2",{attrs:{id:"template-tags"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#template-tags"}},[t._v("#")]),t._v(" Template Tags")]),t._v(" "),e("p",[t._v("Go uses "+t._s(t.and)+" as the default template tags. In the case that these tags conflict with other template tags as in AngularJS, we can use other tags. To do so,\nIn configuration file:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("\ttemplateleft = &lt;&lt;&lt;\n\ttemplateright = &gt;&gt;&gt;\n")])])]),e("p",[t._v("Or, add these to the main.go:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('\tweb.BConfig.WebConfig.TemplateLeft = "&lt;&lt;&lt;"\n\tweb.BConfig.WebConfig.TemplateRight = "&gt;&gt;&gt;"\n')])])]),e("h2",{attrs:{id:"template-data"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#template-data"}},[t._v("#")]),t._v(" Template Data")]),t._v(" "),e("p",[t._v("Template gets its data from "),e("code",[t._v("this.Data")]),t._v(" in Controller. So for example if you write")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{.Content}}\n")])])]),e("p",[t._v("in the template, you need to assign it in the Controller first:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('this.Data["Content"] = "value"\n')])])]),e("p",[t._v("Different rendering types:")]),t._v(" "),e("h3",{attrs:{id:"struct"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#struct"}},[t._v("#")]),t._v(" struct")]),t._v(" "),e("p",[t._v("Struct variable:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("  \ttype A struct{\n  \t\tName string\n  \t\tAge  int\n  \t}\n")])])]),e("p",[t._v("Assign value in the Controller:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('this.Data["a"]=&amp;A{Name:"astaxie",Age:25}\n')])])]),e("p",[t._v("Render it in the template:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("  \tthe username is {{.a.Name}}\n  \tthe age is {{.a.Age}}\n")])])]),e("h3",{attrs:{id:"map"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#map"}},[t._v("#")]),t._v(" map")]),t._v(" "),e("p",[t._v("Assign value in the Controller:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('  \tmp["name"]="astaxie"\n  \tmp["nickname"] = "haha"\n  \tthis.Data["m"]=mp\n')])])]),e("p",[t._v("Render it in the template:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("  \tthe username is {{.m.name}}\n  \tthe username is {{.m.nickname}}\n")])])]),e("h3",{attrs:{id:"slice"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#slice"}},[t._v("#")]),t._v(" slice")]),t._v(" "),e("p",[t._v("Assign value in the Controller:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('  \tss :=[]string{"a","b","c"}\n  \tthis.Data["s"]=ss\n')])])]),e("p",[t._v("Render it in the template:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("  \t{{range $key, $val := .s}}\n  \t{{$key}}\n  \t{{$val}}\n      {{end}}\n")])])]),e("h2",{attrs:{id:"template-name"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#template-name"}},[t._v("#")]),t._v(" Template Name")]),t._v(" "),e("blockquote",[e("p",[t._v("From version 1.6: this.TplNames is this.TplName")])]),t._v(" "),e("p",[t._v("Beego uses Go's built-in template engine, so the syntax is same as Go.  To learn more about template see "),e("a",{attrs:{href:"https://github.com/Unknwon/build-web-application-with-golang_EN/blob/master/eBook/07.4.md",target:"_blank",rel:"noopener noreferrer"}},[t._v("Templates"),e("OutboundLink")],1),t._v(".")]),t._v(" "),e("p",[t._v("You can set the template name in Controller and Beego will find the template file under the viewpath and render it automatically. In the config below, Beego will find add.tpl under admin and render it.")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("this"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("TplName "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin/add.tpl"')]),t._v("\n")])])]),e("p",[t._v("Beego supports "),e("code",[t._v(".tpl")]),t._v(" and "),e("code",[t._v(".html")]),t._v(" file extensions by default. If you're using other extensions, you must set it in the configuration first:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("beego"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("AddTemplateExt")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"file_extension_you_need"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),e("p",[t._v("If "),e("code",[t._v("TplName")]),t._v(" is not set in the Controller while "),e("code",[t._v("autorender")]),t._v(" is enabled, Beego will use "),e("code",[t._v("TplName")]),t._v(" as below by default:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("c"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("TplName "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" strings"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("ToLower")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("controllerName"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/"')]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" strings"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("ToLower")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("actionName"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"."')]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" c"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("TplExt\n")])])]),e("p",[t._v('It is Controller name + "/" + request method name + "." + template extension. So if the Controller name is '),e("code",[t._v("AddController")]),t._v(", request method is "),e("code",[t._v("POST")]),t._v(" and the default template extension is "),e("code",[t._v("tpl")]),t._v(", Beego will render "),e("code",[t._v("/viewpath/addcontroller/post.tpl")]),t._v(" template file.")]),t._v(" "),e("h2",{attrs:{id:"layout-design"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#layout-design"}},[t._v("#")]),t._v(" Layout Design")]),t._v(" "),e("p",[t._v("Beego supports layout design. For example, if in your application the main navigation and footer does not change and only the content part is different, you can use a layout like this:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("this"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Layout "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin/layout.html"')]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("TplName "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin/add.tpl"')]),t._v("\n")])])]),e("p",[t._v("In "),e("code",[t._v("layout.html")]),t._v(" you must set a variable like this:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("{{.LayoutContent}}\n")])])]),e("p",[t._v("Beego will parse the file named "),e("code",[t._v("TplName")]),t._v(" and assign it to "),e("code",[t._v("LayoutContent")]),t._v(" then render "),e("code",[t._v("layout.html")]),t._v(".")]),t._v(" "),e("p",[t._v("Beego will cache all the template files. You can also implement a layout this way:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('\t{{template "header.html"}}\n\tLogic code\n\t{{template "footer.html"}}\n')])])]),e("h2",{attrs:{id:"layoutsection"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#layoutsection"}},[t._v("#")]),t._v(" LayoutSection")]),t._v(" "),e("p",[e("code",[t._v("LayoutContent")]),t._v(" is a little complicated, as it can include Javascript and CSS. Since in most situations having only one "),e("code",[t._v("LayoutContent")]),t._v(" is not enough, there is an attribute called "),e("code",[t._v("LayoutSections")]),t._v(" in "),e("code",[t._v("Controller")]),t._v(". It allows us to set multiple "),e("code",[t._v("section")]),t._v(" in "),e("code",[t._v("Layout")]),t._v(" page and each "),e("code",[t._v("section")]),t._v(" can contain its own sub-template page.")]),t._v(" "),e("p",[t._v("layout_blog.tpl:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;Lin Li&lt;/title&gt;\n    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;\n    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;\n    &lt;link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"&gt;\n    &lt;link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css"&gt;\n    {{.HtmlHead}}\n&lt;/head&gt;\n&lt;body&gt;\n\n    &lt;div class="container"&gt;\n        {{.LayoutContent}}\n    &lt;/div&gt;\n    &lt;div&gt;\n        {{.SideBar}}\n    &lt;/div&gt;\n    &lt;script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"&gt;&lt;\/script&gt;\n    &lt;script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"&gt;&lt;\/script&gt;\n    {{.Scripts}}\n&lt;/body&gt;\n&lt;/html&gt;\n')])])]),e("p",[t._v("html_head.tpl:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("&lt;style&gt;\n     h1 {\n        color: red;\n     }\n&lt;/style&gt;\n")])])]),e("p",[t._v("scripts.tplï¼š")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('&lt;script type="text/javascript"&gt;\n    $(document).ready(function() {\n        // bla bla bla\n    });\n&lt;\/script&gt;\n')])])]),e("p",[t._v("Here is the logic in the Controller:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BlogsController "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\nweb"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Controller\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("this "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BlogsController"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Layout "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"layout_blog.tpl"')]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("TplName "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"blogs/index.tpl"')]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("LayoutSections "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("LayoutSections"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"HtmlHead"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"blogs/html_head.tpl"')]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("LayoutSections"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Scripts"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"blogs/scripts.tpl"')]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("LayoutSections"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Sidebar"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"another-approach"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#another-approach"}},[t._v("#")]),t._v(" Another approach")]),t._v(" "),e("p",[t._v("We can also just specify the template the controller is going to use and let the template system handle the layout:")]),t._v(" "),e("p",[t._v("for example:")]),t._v(" "),e("p",[t._v("controller:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("this"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("TplName "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"blog/add.tpl"')]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Data"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"SomeVar"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"SomeValue"')]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Data"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Title"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Add"')]),t._v("\n")])])]),e("p",[t._v("template add.tpl:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('\t{{ template "layout_blog.tpl" . }}\n\t{{ define "css" }}\n    \t\t&lt;link rel="stylesheet" href="/static/css/current.css"&gt;\n\t{{ end}}\n\n\n\t{{ define "content" }}\n    \t\t&lt;h2&gt;{{ .Title }}&lt;/h2&gt;\n    \t\t&lt;p&gt; This is SomeVar: {{ .SomeVar }}&lt;/p&gt;\n\t{{ end }}\n\n\t{{ define "js" }}\n\t\t&lt;script src="/static/js/current.js"&gt;&lt;\/script&gt;\n\t{{ end}}\n')])])]),e("p",[t._v("layout_blog.tpl:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;{{ .Title }}&lt;/title&gt;\n    &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;\n    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;\n    &lt;link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"&gt;\n    &lt;link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css"&gt;\n     {{ block "css" . }}{{ end }}\n&lt;/head&gt;\n&lt;body&gt;\n\n    &lt;div class="container"&gt;\n        {{ block "content" . }}{{ end }}\n    &lt;/div&gt;\n    &lt;script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"&gt;&lt;\/script&gt;\n    &lt;script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"&gt;&lt;\/script&gt;\n     {{ block "js" . }}{{ end }}\n&lt;/body&gt;\n&lt;/html&gt;\n')])])]),e("p",[t._v("Using "),e("code",[t._v("block")]),t._v(" action instead of "),e("code",[t._v("template")]),t._v(" allows us to have default block content and skipping blocks that we don't need in every template (for example, if we don't need css block in "),e("code",[t._v("add.tpl")]),t._v(" template - we will not define it and that won't raise an error)")]),t._v(" "),e("h2",{attrs:{id:"renderform"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#renderform"}},[t._v("#")]),t._v(" renderform")]),t._v(" "),e("p",[t._v("Define struct:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" User "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\nId    "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("         "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"-"`')]),t._v("\nName  "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"username"`')]),t._v("\nAge   "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("         "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"age,text,age:"`')]),t._v("\nSex   "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\nIntro "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:",textarea"`')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("ul",[e("li",[e("p",[t._v("StructTag definition uses "),e("code",[t._v("form")]),t._v(" as tag. There are three optional params separated by ',':")]),t._v(" "),e("ul",[e("li",[t._v("The first param is "),e("code",[t._v("name")]),t._v(" attribute of the form field. If empty, it will use "),e("code",[t._v("struct field name")]),t._v(" as the value.")]),t._v(" "),e("li",[t._v("The second param is the form field type. If empty, it is assumed as "),e("code",[t._v("text")]),t._v(".")]),t._v(" "),e("li",[t._v("The third param is the tag of form field. If empty, it will use "),e("code",[t._v("struct field name")]),t._v(" as the tag name.")])])]),t._v(" "),e("li",[e("p",[t._v("If the "),e("code",[t._v("form")]),t._v(" tag only has one value, it is the "),e("code",[t._v("name")]),t._v(" attribute of the form field. Except last value can be ignore all the other place must be separated by ','. E.g.: "),e("code",[t._v('form:",,username:"')])])]),t._v(" "),e("li",[e("p",[t._v("To ignore a field there are two ways:")]),t._v(" "),e("ul",[e("li",[t._v("The first way is to use lowercase for the field name in the struct.")]),t._v(" "),e("li",[t._v("The second way is to set "),e("code",[t._v("-")]),t._v(" as the value of "),e("code",[t._v("form")]),t._v(" tag.")])])])]),t._v(" "),e("p",[t._v("controllerï¼š")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("this "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AddController"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Data"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Form"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&amp;")]),t._v("User"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\nthis"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("TplName "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"index.tpl"')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("The param of Form must be a pointer to a struct.")]),t._v(" "),e("p",[t._v("template:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('\t&lt;form action="" method="post"&gt;\n\t{{.Form | renderform}}\n\t&lt;/form&gt;\n')])])]),e("p",[t._v("The code above will generate the form below:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('Name: &lt;input name="username" type="text" value="test"&gt;&lt;/br&gt;\nAge: &lt;input name="age" type="text" value="0"&gt;&lt;/br&gt;\nGender: &lt;input name="Sex" type="text" value=""&gt;&lt;/br&gt;\nIntro: &lt;input name="Intro" type="textarea" value=""&gt;\n')])])])])}),[],!1,null,null,null);e.default=n.exports}}]);</pre></body></html>