当前位置: 首页 > 工具软件 > Echo Go > 使用案例 >

go语言 echo框架_如何在Go Echo Web框架中设置嵌套HTML模板

蒯安平
2023-12-01

go语言 echo框架

by Ying Kit Yuen

英杰苑

如何在Go Echo Web框架中设置嵌套HTML模板 (How to setup a nested HTML template in the Go Echo web framework)

Echo is a lightweight but complete web framework in Go for building RESTful APIs. It is fast and includes a bunch of middleware for handling the whole HTTP request-response cycle. For the rendering part, it works with any template engine, but I use the standard html/template package for the purpose of simplicity. By the end of this article, we will have a nested template Echo project setup.

EchoGo中一个轻量级但完整的Web框架,用于构建RESTful API。 它速度很快,并且包括一堆用于处理整个HTTP请求-响应周期的中间件。 对于呈现部分,它可以与任何模板引擎一起使用,但是出于简化的目的,我使用标准的html / template包。 到本文结尾,我们将有一个嵌套的模板Echo项目设置。

If you already have an idea on how Echo works, jump to the Using a nested template section.

如果您已经对Echo的工作原理有所了解,请跳至使用嵌套模板部分

基本的Echo项目设置 (A basic Echo project setup)

在正确的$ GOPATH下创建项目文件夹 (Create the project folder under the proper $GOPATH)

The complete project code is hosted on Gitlab. First we’ll create the project folder here: $GOPATH/src/gitlab.com/ykyuen/golang-echo-template-example.

完整的项目代码托管在Gitlab上 。 首先,我们将在此处创建项目文件夹: $ GOPATH / src / gitlab.com / ykyuen / golang-echo-template-example

创建main.go (Create the main.go)

Inside the newly created folder, let’s just copy the hello world example from the Echo official site and create the main.go.

在新创建的文件夹中,让我们从Echo官方网站复制hello world示例并创建main.go。

main.go

main.go

使用dep下载Echo软件包 (Download the Echo package using dep)

Simply run dep init if dep is installed. You can refer to this post for more information about dep: Manage Go dependencies using dep.

如果简单地运行DEP初始化 DEP安装。 您可以参考这篇文章以获取有关dep的更多信息: 使用dep管理Go依赖项

Or run go get github.com/labstack/echo to download the Echo package in $GOPATH.

或者运行go get github.com/labstack/echo来下载$ GOPATH中Echo包。

运行你好世界 (Run the hello world)

Start the application by typing go run main.go and then visit http://localhost:1323 through the browser or the curl command.

通过键入go run main.go来启动应用程序,然后通过浏览器或curl命令访问http:// localhost:1323

返回一个JSON响应 (Return a JSON response)

When building a RESTful API, it is more likely that the client wants to receive a JSON response instead of a string. Let’s write some Go code in main.go.

构建RESTful API时,客户端更有可能希望接收JSON响应而不是字符串。 让我们在main.go中编写一些Go代码。

main.go

main.go

返回HTML (Return an HTML)

Similar to returning a JSON object, we just need to call another method in the return statement.

与返回JSON对象类似,我们只需要在return语句中调用另一个方法。

main.go

main.go

The above are just two simple examples. Echo has a few more convenient ways to return JSON and HTML. For details please refer to the documentation.

以上只是两个简单的示例。 Echo有一些更方便的方法来返回JSON和HTML。 有关详细信息,请参阅文档

使用模板引擎渲染HTML (Render HTML using template engine)

As mentioned at the very beginning, we could implement a template engine when returning the HTTP response. But before that, let’s restructure the project as follows:

如开头所述,我们可以在返回HTTP响应时实现模板引擎。 但是在此之前,让我们按如下所示重组项目:

main.go

main.go

In this main.go, we define a type called TemplateRegistry and implement the Renderer interface. A Renderer is a simple interface which wraps the Render() function. Inside a TemplateRegistry instance, it has a templates field containing all the templates needed for the Echo server to render the html response, and this is configured in the main() flow.

在这个main.go中 ,我们定义一个名为TemplateRegistry的类型并实现Renderer接口。 Renderer是包装Render()函数的简单接口。 在TemplateRegistry实例内部,它具有一个template字段,其中包含Echo服务器呈现html响应所需的所有模板,并且在main()流中对其进行配置。

On the other hand, we define the HomeHandler in order to keep the logic in a separate file.

另一方面,我们定义HomeHandler以便将逻辑保存在单独的文件中。

handler/home_handler.go

handler / home_handler.go

When the c.Render() is invoked, it executes the template which is already set in our TemplateRegistry instance as stated in main.go. The three parameters are:

调用c.Render()时 ,它将执行在main.go中所述的TemplateRegistry实例中已经设置的模板 。 这三个参数是:

  1. HTTP response code

    HTTP响应码
  2. The template name

    模板名称
  3. The data object which could be used in the template

    可以在模板中使用的数据对象

view/home.html

view / home.html

This above template is named home.html as stated in the define statement. It can read the name and msg strings from c.Render() for the <title>; and <h1> tags.

如上define语句中所述,以上模板名为home.html 。 它可以从c.Render()中读取<tit le>的名称msg字符串 d <h1>标记。

使用嵌套模板 (Using nested template)

In the above setup, every HTML template has a complete set of HTML code and many of them are duplicated. Using nested templates makes it easier to maintain the project.

在以上设置中,每个HTML模板都有一套完整HTML代码,其中许多都是重复的。 使用嵌套模板可以更轻松地维护项目。

Originally the templates field in the TemplateRegistry contained all the templates files. In the new setup, we made it into an array field and each array item is a single set of template files for a particular HTML page.

最初, TemplateRegistry中的模板字段包含所有模板文件。 在新设置中,我们将其放入一个数组字段,并且每个数组项都是针对特定HTML页面的一组模板文件。

We add a few files to the project and it should look like this:

我们向项目添加了一些文件,它看起来应该像这样:

The code below is based on this gist created by rand99.

下面的代码就是基于这一主旨所创造rand99

main.go

main.go

We add a new route /about which is handled by an AboutHandler. As you can see from the above lines 34-36, the templates array contains different sets of template files for different HTML pages. The Render() takes the name parameter as the templates array key so it can execute the correct template set.

我们添加一个新的路由/ about ,由AboutHandler处理 从上面的第34-36行可以看到, 模板数组包含用于不同HTML页面的不同模板文件集。 Render()name参数作为模板数组键,以便它可以执行正确的模板集。

view/base.html

view / base.html

The template statement tells the template engine that it should look for the {{title}} and {{body}} definitions in the template set, and they are defined in the home.html as well as about.html.

template语句告诉模板引擎它应该在模板集中查找{{title}}{{body}}定义,它们在home.htmlabout.html中定义。

view/about.html

查看/about.html

And here is the AboutHanlder which has no big difference from the HomeHandler.

这里是具有从HomeHandler没有什么大的区别AboutHanlder。

handler/about_handler.go

handler / about_handler.go

摘要 (Summary)

This is just a basic example implementing nested templates using the Go standard html/template library in Echo. With proper setup, we could develop a more customized and convenient pattern for Echo or even make it work with any other template engines.

这只是使用Echo中Go标准html / template库实现嵌套模板的基本示例。 通过适当的设置,我们可以为Echo开发更加自定义和方便的模式,甚至使其与任何其他模板引擎一起使用。

The complete example can be found on gitlab.com.

完整的示例可以在gitlab.com找到

— Originally posted on Boatswain Blog.

—最初发布在Boatswain博客上

翻译自: https://www.freecodecamp.org/news/how-to-setup-a-nested-html-template-in-the-go-echo-web-framework-670f16244bb4/

go语言 echo框架

 类似资料: