I went to the beego documentation site, Views -> Template Parsing , the section "Another Aproach", trying to make an example from this section. Created a new project using bee tool. In order not to create a new controller and router has changed the existing default. File controller / default.go:

package controllers import ( "github.com/astaxie/beego" ) type MainController struct { beego.Controller } func (this *MainController) Get() { this.TplName = "blog/add.tpl" this.Data["SomeVar"] = "SomeValue" this.Data["Title"] = "Add" } 

File views / blog / add.tpl:

 {{ template "layout_blog.tpl" . }} {{ define "css" }} <link rel="stylesheet" href="/static/css/current.css"> {{ end}} {{ define "content" }} <h2>{{ .Title }}</h2> <p> This is SomeVar: {{ .SomeVar }}</p> {{ end }} {{ define "js" }} <script src="/static/js/current.js"></script> {{ end}} 

File views / layout_blog.tpl:

 <!DOCTYPE html> <html> <head> <title>Lin Li</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"> <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css"> {{ block "css" . }}{{ end }} </head> <body> <div class="container"> {{ block "content" . }}{{ end }} </div> <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> {{ block "js" . }}{{ end }} </body> </html> 

In general, I did everything as indicated in the example, but when I started it, nothing was displayed, it is displayed at intervals of page refresh (you need to refresh the page several times to see the result). The problem in the "{{define}}" and "{{block}}" of the template does not work ... Is it just me or did I do something wrong?

For clarity, recorded a 2 minute video: https://www.screencast.com/t/tjAEAtoT

  • Looks like a BeeGo bug. Try compiling the project with -race and see if there are any races. Well, a little advice: it is better to start studying web applications on go from the standard library, and not from the frameworks. - Ainar-G
  • I now do not understand what the default template engine does not suit. It is beautiful, there is everything you need to develop. Why use beego, a trash framework with a terribly slow multiplexer. github.com/spouk/spoukfw/blob/master/spoukrender.go here is my example of implementing my own renderer with reloading from the standard template engine with my own filters, for my own framework (I wrote to check my own systems in this aspect). - Spouk
  • And also, if you still think to use the framework, then use Echo. echo.labstack.com its implementation of a multiplexer on the radix tree is good for medium and small projects and everything else is in order for implementation. - Spouk
  • @Spouk At first glance, Echo is a copy-paste Gin framework - bsbak
  • @Spouk At beego, everything is fine with speed, yes beego.Router () is slow, but beego.Get () is almost as good as httpRouter that you have in spouk, look at how your SessionSpouk file is written and how sessions in beego are implemented. .. And in general, I looked through the beego files and I can say that I like the way it was written ... - bsbak

0