Go:在模板文件中打印URI(使用echo框架)

huangapple go评论81阅读模式
英文:

Go: print URI in template files (echo framework)

问题

我正在使用echo框架。当我尝试在模板文件中打印URI :mynumber作为变量时,我遇到了困难。

除了URI :mynumber之外,所有变量都正常工作。我不知道如何将:mynumberNameAge一起包含在内。

以下是我的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)
})

huangapple
  • 本文由 发表于 2015年4月30日 10:44:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/29958132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定