templates not working for me

199 views
Skip to first unread message

joseph.p...@gmail.com

unread,
Feb 4, 2024, 2:40:40 AM2/4/24
to golang-nuts
I'm goofing around with GIN and trying some examples. I can't get templating to work for me.

File inclusion works, but variable substitution does not.

GOLANG version 1.20.12

package main


import "github.com/gin-gonic/gin"

import "net/http"

// import "gorm.io/driver/sqlite"

// import "gorm.io/gorm"


func main() {


        server := gin.Default()


        server.Static("/stylesheets", "./")


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


        server.GET("/test", func(ctx *gin.Context) {

                ctx.JSON(200, gin.H{

                        "message": "OK!!",

                })

        })

        server.GET("/temp", func(ctx *gin.Context) {

                ctx.HTML(http.StatusOK, "index.html", gin.H{

                        "title": "Main website",

                        "foo": "bar",

                })

        })

        server.GET("/index", func(ctx *gin.Context) {

                ctx.JSON(200, gin.H{

                        "message": "OK!!",

                })

        })

        server.Run(":8080")

}


Specifically, opening a browser and accessing the /temp endpoint doesn't change the title or print the text of 'foo'

templates/header.html:

<!DOCTYPE html>

   <title>{{.title}}</title>

  <head>

   <link href="https://cdn.jsdelivr.net/npm/boot...@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">

 </head>


 <body class="container">



What's going on here?


joseph.p...@gmail.com

unread,
Feb 8, 2024, 1:11:32 AM2/8/24
to golang-nuts
Ok,

Variable substitution doesn't seem to work in an included template file. Is that a known limitation?

Ian Lance Taylor

unread,
Feb 8, 2024, 6:37:38 PM2/8/24
to joseph.p...@gmail.com, golang-nuts
On Wed, Feb 7, 2024 at 10:11 PM joseph.p...@gmail.com
<joseph.p...@gmail.com> wrote:
>
> Variable substitution doesn't seem to work in an included template file. Is that a known limitation?

golang-nuts is for general discussion of Go, its tools, and its
standard library. It's fine to ask about the Gin web framework here,
but you are likely to get better answers if you ask in a forum that is
focused on Gin.

Ian
Reply all
Reply to author
Forward
0 new messages