英文:
Go: print URI in template files (echo framework)
问题
我正在使用echo
框架。当我尝试在模板文件中打印URI :mynumber
作为变量时,我遇到了困难。
除了URI :mynumber
之外,所有变量都正常工作。我不知道如何将:mynumber
与Name
和Age
一起包含在内。
以下是我的server.go
代码:
package main
import (
"github.com/labstack/echo"
"html/template"
"io"
"net/http"
)
type Person struct {
Name, Age, Mynumber string
}
type (
Template struct {
templates *template.Template
}
)
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
e := echo.New()
p := Person{Name: "Mike San", Age: "35"}
t := &Template{
templates: template.Must(template.ParseFiles("public/views/testhere.html")),
}
e.Renderer(t)
e.Get("/testing/:mynumber", func(c *echo.Context) {
c.Render(http.StatusOK, "onlytestingtpl", p)
})
e.Run(":4444")
}
以下是public/views/testhere.html
代码:
{{define "onlytestingtpl"}}My name is {{.Name}}. I'm {{.Age}} years old. My number is {{.Mynumber}}.{{end}}
供您参考,以下是不使用模板文件打印URI的示例代码:
package main
import (
"github.com/labstack/echo"
"net/http"
)
func main() {
e := echo.New()
e.Get("/users/:id", func(c *echo.Context) {
id := c.Param("id")
c.String(http.StatusOK, "My number is "+id)
})
e.Run(":4444")
}
英文:
I'm using echo
framework. I'm stuck when trying to print URI :mynumber
as variable in template files.
All variable working fine except the URI :mynumber
. I have no idea how to include :mynumber
together with Name
& Age
Below is my server.go
:
package main
import (
"github.com/labstack/echo"
"html/template"
"io"
"net/http"
)
type Person struct {
Name, Age, Mynumber string
}
type (
Template struct {
templates *template.Template
}
)
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
e := echo.New()
p := Person{Name: "Mike San", Age: "35"}
t := &Template{
templates: template.Must(template.ParseFiles("public/views/testhere.html")),
}
e.Renderer(t)
e.Get("/testing/:mynumber", func(c *echo.Context) {
c.Render(http.StatusOK, "onlytestingtpl", p)
})
e.Run(":4444")
}
Below is public/views/testhere.html
:
{{define "onlytestingtpl"}}My name is {{.Name}}. I'm {{.Age}} years old. My number is {{.Mynumber}}.{{end}}
For your reference, below is example is print URI without template file:
package main
import (
"github.com/labstack/echo"
"net/http"
)
func main() {
e := echo.New()
e.Get("/users/:id", func(c *echo.Context) {
id := c.Param("id")
c.String(http.StatusOK, "My number is "+id)
})
e.Run(":4444")
}
答案1
得分: 1
你只需要按照你的工作示例从URL中检索它:
number := c.Param("mynumber")
然后将其设置在你传递的Person
实例上:
p.Mynumber = number
这将导致:
e.Get("/testing/:mynumber", func(c *echo.Context) {
number := c.Param("mynumber")
p.Mynumber = number
c.Render(http.StatusOK, "onlytestingtpl", p)
})
英文:
You simply need to retrieve it from the URL as per your working sample:
number := c.Param("mynumber")
And set it on the Person
instance you're passing in:
p.Mynumber = number
Which would result in:
e.Get("/testing/:mynumber", func(c *echo.Context) {
number := c.Param("mynumber")
p.Mynumber = number
c.Render(http.StatusOK, "onlytestingtpl", p)
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论