Ad Code

Parsing Different Types of Data to the Template in Beego Framework

     


Watch on Youtube:  Parsing Different Types of Data to the Template in Beego Framework

Hello friends! In this post, I will discuss about how to parse different data types into template in Beego application. Friends, when are a developing a full stack web application, then many times, we need to pass collection of data. In Golang, we generally deal with three group of data:

a. struct

b. map 

c. slice

    In this post, I will show you how we can pass these data types into the template. Let us start with struct.

First of all, in the controller file, let us create a struct Person.

type Product struct {
Name string
Price int
}

Now, let us define a controller function hello() like below:


func (c* MainController) Hello() {}


We will assign some values to the struct Product and pass them through

this Hello() function.


func (c* MainController) Hello() {
c.Data["product"] = &Product{
Name : "Phone",
Price : 800,
}
}


After this, we will render a template file through this function:


c.TplName = "hello.tpl"



The whole controller code is like below:


func (c* MainController) Hello() {
c.Data["product"] = &Product{
Name : "Phone",
Price : 800,
}
c.TplName = "hello.tpl"
}

Now, we have to create a template file inside the view directory.
We have to name it as: hello.tpl.


We will retrieve the data in this template file that we are sending from
controller function.

{{.product.Name}}
{{.product.Price}}

At the end, we have to define router:

beego.Router("/hello", &controllers.MainController{}, "get:Hello")

That's it guys. Now start your application using bee run command and hit
the url. You will be able to see the struct data.

Similarly, you can parse map data and slice data. Let us see how we can
parse map data. In the Hello() function itself, add below code to create
a map and parse it:


mp := map[string]interface{} {
"name" : "beego",
"age" : 100,
}
c.Data["framework"] = mp

We can retrieve above map data in template file like below:

{{.framework.name}}
{{.framework.age}}

Now, to parse slice, add below code in Hello() function:

languages := []string{"Golang", "Java"}
c.Data["languages"] = languages

Add below code in template file to get above mentioned slice data:

{{range $key, $value := .languages}} {{$key}} {{$value}}
{{end}}

The whole code is like below:


1. default.go

type Product struct {
Name string
Price int
}


func (c* MainController) Hello() {
c.Data["product"] = &Product{
Name : "Phone",
Price : 800,
}

mp := map[string]interface{} {
"name" : "beego",
"age" : 100,
}
c.Data["framework"] = mp

languages := []string{"Golang", "Java"}
c.Data["languages"] = languages

c.TplName = "hello.tpl"
}

2. hello.tpl

<div align = "middle"> <h1> Hello Beego </h1> <br> Showing struct data: <br> The name of product is: {{.product.Name}} <br> The price of product is: {{.product.Price}} <br> <br> Showing map data: <br> The name of the framework is : {{.framework.name}} <br> The age of the framework is : {{.framework.age}} <br> <br> Showing slice data: <br> {{range $key, $value := .languages}} {{$key}} {{$value}} <br> {{end}} </div>

3. router.go

beego.Router("/hello", &controllers.MainController{}, "get:Hello")