Golang Web: Setting Up GIN Framework with Pongo2 template engine on GAE Standard env


What Is Gin

Gin is a web framework written in Golang. It features a martini-like API with much better performance, up to 40 times faster. If you need performance and good productivity, you will love Gin.

What Is Pongo

Pongo is a well-tested template engine which implements a Django template like syntax and pongo2 is the successor of pongo, a Django-syntax like templating-language.

For Pongo documentation on how the templating language works you can head over to the Django documentation. pongo2 aims to be compatible with it (link).

Ok then. let’s start the journey

Installing GIN

go get github.com/gin-gonic/gin

go get github.com/gin-gonic/gin

Installing Pongo2

go get -u github.com/flosch/pongo2 go get -u github.com/flosch/pongo2

Install robvdl/pongo2gin

Package pongo2gin is a template renderer that can be used with the Gin web framework gin-gonic/gin it uses the Pongo2 template library flosch/pongo2 to use pongo2gin you need to set your router.HTMLRenderer to a new renderer instance, this is done after creating the Gin router when the Gin application starts up. You can use pongo2gin.Default() to create a new renderer with default options, this assumes templates will be located in the “templates” directory, or you can use pongo2.New() to specify a custom location.To render templates from a route, call c.HTML just as you would with regular Gin templates, the only difference is that you pass template data as a pongo2.Context instead of gin.H type.

Create app.yml

  runtime: go
  api_version: go1 
  
  handlers:
  - url: /.* 
    script: _go_app
    secure: always
    redirect_http_response_code: 301 

Create main.go

 package main

  import (
      "net/http"

      "github.com/flosch/pongo2"
      "github.com/gin-gonic/gin"
      "github.com/robvdl/pongo2gin"
  )

  func init() {
      r := gin.Default()

      r.HTMLRender = pongo2gin.Default()

      r.LoadHTMLGlob("templates/*.html")

      r.GET("/", func(c *gin.Context) {
          c.HTML(http.StatusOK, "main.html", pongo2.Context{
              "title":    "Hello world !",
              "greating": "Hi, Hello world my mate!",
          })
      })

      http.Handle("/", r)
  }

Create templates/main.html

  <html>
      <head>
          <title>
              {{ .title }}
          </title>
      </head>
      <body>
          {{ .greating }}
      </body>
    
  </html>

Run App engine development server

dev_appserver.py –port=9999 ./The result

Deployment to GAE using gcloud command

gcloud app deploy –verbosity=info

The Final Result On Browser

50.result

Resources: link1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s