The web template system

In web programming, a web template is a text document that describes the format in which a web page should appear to the user. In this book, we will focus on web templates from Go's html/template package—the package that implements data-driven templates suitable for use in web applications.

Web templates (we'll refer to them simply as templates moving forward) are text documents, that are typically implemented in HTML, and may contain special commands that are embedded inside of them. In Go, we refer to these commands as actions. We denote actions in templates by placing them inside a pair of opening and closing double curly braces—{{ and }}.

Templates form the means to present data to the user in a manner that is intuitive and acceptable. In fact, you can think of a template as the means by which we dress up data. 

In this book, we will use the file extension of .tmpl to designate Go template source files. You may notice that some other Go projects use the file extension of .html instead. There is no hard set rule to prefer one extension over the other, just remember that once you choose which file extension to use, it's best to stick with it to promote uniformity in the project codebase.

Templates are used in conjunction with a web template system. In Go, we have the robust html/template package from the standard library to render templates. When we use the term render template, we refer to the process by which one or more templates, along with a data object, is processed through a template engine that generates HTML web page output, as depicted in Figure 4.1:

Figure 4.1: How a web page is rendered by a web template system

The key components in Figure 4.1, the template engine, the template data object, and the templates, can be classified as comprising a web template system. Each component plays an important role toward rendering the web page output, and in the following sections, we'll consider the role that each component plays in this process of producing the HTML output to display in the web browser. In this chapter, we will build IGWEB's About page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.35.185