Comments (5)
Please add the expected effect and actual effect, at present, I can't judge your needs by looking at the issue.
from gin.
Please add the expected effect and actual effect, at present, I can't judge your needs by looking at the issue.
Hi,Edited . As you can see in the Actual result the "script" codes were missed.
from gin.
HTML natively supports <script> elements.
You shouldn't use {{}} this way; {{}} is used as a key to match replacement text.
follow the steps below to delete, you can achieve your desired effect:
- {{/*
<script>
var x;
var txt="";
var person={{.JsonParam}}
for (x in person){
txt=txt + person[x].a;
}
document.getElementById("demo").innerHTML=txt;
</script>
- */}}
There is problem with your javascript usage. You need to use a struct to specify an object when using a template.
pls check your javascript code.
Code that can be used for reference:
directory structure:
.
├── main.go
└── templates
└── test.tmpl
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
type JsonParam struct {
X int64 `json:"x"`
Y int64 `json:"y"`
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
router.GET("/test", func(c *gin.Context) {
c.HTML(http.StatusOK, "test.tmpl", gin.H{
"title": "test",
"MyParam":"MyParam",
"JsonParam": []JsonParam{{X: 1,Y: 2},{X: 3,Y: 4}},
})
})
router.Run(":8080")
}
{{define "test.tmpl"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>posts/index</title>
</head>
<body>
<h1>{{.title}}</h1>
<h3>{{.MyParam}}</h3>
<h3>{{.JsonParam}}</h3>
<div id="demo"></div>
<script>
var txt = 0;
var person={{.JsonParam}}
for (item of person){
txt = txt + item.x;
console.log(item);
}
console.log(txt);
document.getElementById("demo").innerHTML=txt;
</script>
</body>
</html>
{{end}}
from gin.
HTML natively supports <script> elements.
You shouldn't use {{}} this way; {{}} is used as a key to match replacement text.
follow the steps below to delete, you can achieve your desired effect:
- {{/* <script> var x; var txt=""; var person={{.JsonParam}} for (x in person){ txt=txt + person[x].a; } document.getElementById("demo").innerHTML=txt; </script> - */}}There is problem with your javascript usage. You need to use a struct to specify an object when using a template.
pls check your javascript code.
Code that can be used for reference:
directory structure:
. ├── main.go └── templates └── test.tmpl
package main import ( "net/http" "github.com/gin-gonic/gin" ) type JsonParam struct { X int64 `json:"x"` Y int64 `json:"y"` } func main() { router := gin.Default() router.LoadHTMLGlob("templates/*") router.GET("/test", func(c *gin.Context) { c.HTML(http.StatusOK, "test.tmpl", gin.H{ "title": "test", "MyParam":"MyParam", "JsonParam": []JsonParam{{X: 1,Y: 2},{X: 3,Y: 4}}, }) }) router.Run(":8080") }{{define "test.tmpl"}} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>posts/index</title> </head> <body> <h1>{{.title}}</h1> <h3>{{.MyParam}}</h3> <h3>{{.JsonParam}}</h3> <div id="demo"></div> <script> var txt = 0; var person={{.JsonParam}} for (item of person){ txt = txt + item.x; console.log(item); } console.log(txt); document.getElementById("demo").innerHTML=txt; </script> </body> </html> {{end}}
Thanks,It was resloved!
from gin.
If the issue has been resolved,pls close the issue
from gin.
Related Issues (20)
- custom time.Time type can not use bindQuery get a value. HOT 3
- configure server parameters HOT 1
- The binding:"required" tag does not seem to work for struct types HOT 3
- Is there a way to preserve order in JSON on response? HOT 1
- redirect is not success HOT 1
- I want to use shouldmindbodywith first and then use formafile, but it doesn't work right now HOT 3
- Extend the routing tree entry
- Transparent decompression for gzip, deflate, etc? HOT 1
- Custom time.Time type can not use bindQuery get a value 2 HOT 2
- http: response.Write on hijacked connection from github.com/gin-gonic/gin.(*responseWriter).Write (response_writer.go:83) HOT 1
- Suggestion: Move Print Debug to Run() functions? HOT 7
- AbortWithStatusJSON doesn't abort context HOT 2
- I translated `tree.go` into java HOT 1
- GetUint and GetUint64 wrong return HOT 1
- Multipart form-data request with empty key for single file upload
- gin with golang 1.21, 1.22 DATA RACE
- gin detects `DATA RACE` with Go1.21, Go1.22 HOT 8
- Please consider remove github.com/bytedance/sonic from dependencies HOT 4
- send response from middleware, continue to execute handler chain HOT 3
- StringToBytes is slower than the raw convertion
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gin.