Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue when using on Gin Framework #11

Open
sujit-baniya opened this issue May 12, 2020 · 7 comments
Open

Issue when using on Gin Framework #11

sujit-baniya opened this issue May 12, 2020 · 7 comments

Comments

@sujit-baniya
Copy link

sujit-baniya commented May 12, 2020

I'm trying to use goview on Gin Framework.

I did following from one of the example:

r.HTMLRender = ginview.New(goview.Config{
		Root:      "./fe/views",
		Extension: ".html",
		Master:    "layouts/master",
		Partials:  []string{},
		Funcs: template.FuncMap{
			"copy": func() string {
				return time.Now().Format(strconv.Itoa(time.Now().Year()))
			},
		},
		DisableCache: false,
	})

master.html:

<!-- /views/admin/master.html -->
<!doctype html>

<html>
<head>
    <title>{{.title}}</title>
    {{template "head" .}}
</head>

<body>
{{template "content" .}}
<hr>
{{include "layouts/footer"}}
</body>
</html>

But when trying to access the page, I stumbled with following error:

User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36


ViewEngine execute template error: html/template:layouts/master:11:11: no such template "content"
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:842 (0x155efa2)
        (*Context).Render: panic(err)
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:851 (0x155f048)
        (*Context).HTML: c.Render(code, instance)
/Users/sujit/Sites/labs/gin-rest/be/app/app.go:43 (0x157c533)
        Serve.func2: ctx.HTML(http.StatusOK, "index", gin.H{
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x155b42a)
        (*Context).Next: c.handlers[c.index](c)
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:83 (0x156eb2f)
        RecoveryWithWriter.func1: c.Next()
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x155b42a)
        (*Context).Next: c.handlers[c.index](c)
/Users/sujit/Sites/labs/gin-rest/be/app/Http/Middleware/request_id.go:23 (0x1572938)
        RequestId.func1: c.Next()
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x155b42a)
        (*Context).Next: c.handlers[c.index](c)
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:83 (0x156eb2f)
        RecoveryWithWriter.func1: c.Next()
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x155b42a)
        (*Context).Next: c.handlers[c.index](c)
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:241 (0x156dc60)
        LoggerWithConfig.func1: c.Next()
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:161 (0x155b42a)
        (*Context).Next: c.handlers[c.index](c)
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:409 (0x1565205)
        (*Engine).handleHTTPRequest: c.Next()
/Users/sujit/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:367 (0x156491c)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/Users/sujit/go/go1.14.2/src/net/http/server.go:2807 (0x12d4072)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/Users/sujit/go/go1.14.2/src/net/http/server.go:1895 (0x12cf9eb)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/Users/sujit/go/go1.14.2/src/runtime/asm_amd64.s:1373 (0x1064870)
        goexit: BYTE    $0x90   // NOP

Shouldn't it work even though template "content" . exists

@foolin
Copy link
Owner

foolin commented May 12, 2020

You need at reader page define content like this:

{{define "content"}}
    <h1 class="hello">This is content!!!!</h1>
    <p>123 + 333 = {{call $.add 123 333}}</p>
    <hr>
    <p><a href="/page">Page render</a></p>
{{end}}

See the example: https://github.com/foolin/goview/blob/master/_examples/gin/views/index.html

@sujit-baniya
Copy link
Author

Should not it be better to not throw exception and skip those if not provided in child file like index.html

@foolin
Copy link
Owner

foolin commented May 13, 2020

Use block tag replace {{template "content" .}}:

{{block "content" .}}Default Content{{end}}

@sujit-baniya
Copy link
Author

Thanks! will try it out.
Any idea how to embed the templates in binary?

@foolin
Copy link
Owner

foolin commented May 13, 2020

@sujit-baniya
Copy link
Author

Thanks! it's working now... but now having some issue with embeding css/js/images.

My folder structure is:

/template
    /layouts
        master.html
    page.html
/public
    /assets
        app.css

I've tried following code to link template and CSS/JS/IMAGES inside public folder usin go rice.

router := gin.Default()
	box := rice.MustFindBox("public")
	cssFileServer := http.StripPrefix("/", http.FileServer(box.HTTPBox()))
	http.Handle("/pbc", cssFileServer)
	//new template engine
	basic := gorice.NewWithConfig(rice.MustFindBox("templates"), goview.Config{
		Root:      "templates",
		Extension: ".html",
		Master:    "layouts/master",
		Partials:  []string{"partials/ad"},
		Funcs: template.FuncMap{
			"copy": func() string {
				return time.Now().Format("2006")
			},
		},
		DisableCache: true,
	})
	router.HTMLRender = ginview.Wrap(basic)

	router.GET("/", func(ctx *gin.Context) {
		// `HTML()` is a helper func to deal with multiple TemplateEngine's.
		// It detects the suitable TemplateEngine for each path automatically.
		ginview.HTML(ctx, http.StatusOK, "index", gin.H{
			"title": "Frontend title!",
		})
	})

	http.ListenAndServe(":8080", router)

I tried to run above query but unable to link CSS file in template
What will be the link for CSS that would be used in template file?
Will it be

/public/assets/app.css OR
/pbc/assets/app.css OR
/assets/app.css

@foolin
Copy link
Owner

foolin commented May 14, 2020

Modify to http.StripPrefix("/pbc", http.FileServer(box.HTTPBox()))

cssFileServer := http.StripPrefix("/pbc", http.FileServer(box.HTTPBox()))
http.Handle("/pbc", cssFileServer)

The link is:

/pbc/assets/app.css

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants