Nesting Templates and Layouts with Go Web Programming

0


WWhat exactly are layouts?

Layouts are fixed templates in the design of web pages that can be reused for multiple pages. Web applications often use page layouts because the pages in a web application must look the same for a consistent user interface. For example, many web app designs have a header menu as well as a footer that provides additional information such as status or copyright or contact details. Other layouts include a left navigation bar or multi-level navigation menus. It is easy to see that layouts can be implemented with nested models.

Models can be nested using the include action. While that seems like all we need, if you start to write a more complicated web application, you will find that you might end up with a lot of hard-coding in your manager and a lot of template files.

Why is this so?

Remember that the syntax for the include action is as follows:

{{pattern “name”. }}

Where “name” is the name of the model and it is a string constant. This means that if we default to the file name as the template name, it will be impossible to have one or two common layouts, since each page will have its own layout template file, which goes to the defeats the purpose of having layouts in the first place. For example, it won’t work as a common layout template file.

Listing 1 – An impractical layout file

Go to web programming

{{template “content.html”}}

The answer to this dilemma is that the Go model engine doesn’t work that way. While we can make each template file define a single template, where the template name is the name of the file, we can actually explicitly define a template in a template file using the define action. Let’s take a look at our layout.html now.

Listing 2 – Explicitly defining a model

{{define “layout”}}

Go to web programming

{{template “content”}}

{{ to finish }}

Note that we start the file with {{define “layout”}} and end the file with {{ to finish }}. Everything in these two action tags is considered part of the layout template. This means that if we have another action tag defined after {{ to finish }} we can actually define another model! In other words, we can define multiple models in the same model file.

Listing 3 – Defining Multiple Models in a Single Model File

{{define “layout”}}

Go to web programming

{{template “content”}}

{{ to finish }}

{{define “content”}}

Hi world!

{{ to finish }}

SEO 4 – Use of explicitly defined models

func process (w http.ResponseWriter, r * http.Request) {

t, _: = template.ParseFiles (“layout.html”)

t.ExecuteTemplate (w, “layout”, “”)

}

The analysis of the model file is always the same, but this time if we want to run the model we have to be more explicit and use the ExecuteModel method, with the name of the model we want to run as the second parameter. The layout model nests the content model, so if we run the layout model we will see Hi world! displayed on the browser. Let’s use curl to get the actual HTML code so we can see it properly.

> curl -i http://127.0.0.1:8080/process

HTTP / 1.1 200 OK

Date: Sun 08 February 2015 14:09:15 GMT

Content-Length: 187

Content type: text / html; character set = utf-8

Go to web programming

Hi world!

Conversely, we can also define the same model in multiple model files. To see how it works, let’s remove the content model definition in layout.html and place it in red_hello.html.

List 5 – red_hello.html

{{define “content”}}

Hello everybody !

{{ to finish }}

Now create a blue_hello.html template file.

List 6 – blue_hello.html

{{define “content”}}

Hello everybody !

{{ to finish }}

Note that we just defined the content model in two places. How can we use these two models? Let’s take a look at our modified handler.

Listing 7 – Handler Using the Same Template in Different Template Files

func process (w http.ResponseWriter, r * http.Request) {

rand.Seed (time.Now (). Unix ())

var t * model.Model

if rand.Intn (10)> 5 {

t, _ = template.ParseFiles (“layout.html”, “red_hello.html”)

} other {

t, _ = template.ParseFiles (“layout.html”, “blue_hello.html”)

}

t.ExecuteTemplate (w, “layout”, “”)

}

Note that we are actually analyzing different model files (i.e. red_hello.html Where hello_bleu.html) according to the random number we create. We use the same disposition model as before, which includes a content model. Remember that the content template is defined in two different files. The actual model we are using now depends on the model file we are analyzing, because these two model files define the same model. In other words, we can change content by parsing different template files, while keeping the same template to be nested in the layout.

If we now recompile our server, start it and access it through the browser, we will randomly see a blue or a red Hi world! appearing on the browser.

You can read more in my book Go Web Programming, available from Manning Publications, Inc.

This article is taken from Go Web Programming by Manning Publishing

Share.

Leave A Reply