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.

Echo是Go中一个轻量级但完整的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 codeHTTP响应码
  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框架

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

  1. linux bash 变量_如何在Linux上的Bash中设置环境变量

    linux bash 变量 fatmawati achmad zaenuri/Shutterstock Fatmawati achmad zaenuri / Shutterstock There's ...

  2. java entitymanager类_如何在Java JDBC EntityManagerFactory类中设置实体的ID?

    我用MySQL Workbench创建了我的MySQL数据库,然后添加了MySQL JDBC Driver和EclipseLink库,然后从数据库创建了实体类;所有使用NetBeans.由于我的观点和 ...

  3. 如何在 ASP.NET Web API 中设置下载文件名 ?

    咨询区 Tae-Sung Shin: 在我的 ApiController 类中,有一个下载文件的Action方法,代码如下: public HttpResponseMessage Get(int id ...

  4. iPhone App创建与审核步骤二:如何在developer.apple.com网站中设置App预览和截屏以完成App上架

    iPhone App创建与审核步骤二:如何在developer.apple.com网站中设置App预览和截屏以完成App上架,根据图标规范RAD Studio 10.4 for delphi XE 或 ...

  5. android自定义videoview,android-如何在播放前在videoview中设置预览图像

    android-如何在播放前在videoview中设置预览图像 我在活动中创建了VideoView,下面是代码. VideoView vvVideos = (VideoView) rootView.f ...

  6. linux echo命令_如何在Linux上使用Echo命令

    linux echo命令 Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock The echo c ...

  7. rola物联网框架_如何搭建一个物联网系统框架?

    下面将谈到几个关键问题: 设备如何接入网络? 设备间如何通信? 物联网数据的用途? 如何搭建起一个物联网系统框架呢?它的技术架构又是怎么样呢? 物联网终端软件系统架构? 物联网云平台系统架构? 1.物 ...

  8. java rest框架_比较Java REST文档框架

    java rest框架 决定在记录REST API时选择哪种Java框架可能很麻烦. 在本博文中,我们将简要比较我们自己使用的REST Web服务的三个文档框架,以及它们如何与Spring框架(这是F ...

  9. executor线程池框架_如何使用Java 5 Executor框架创建线程池

    executor线程池框架 Java 5以Executor框架的形式在Java中引入了线程池,它允许Java程序员将任务提交与任务执行分离. 如果要使用Java进行服务器端编程,则线程池是维护系统可伸 ...

最新文章

  1. 艰难就业季,2020 AI算法岗春招汇总 面经大全来了!!!
  2. Linux平台Qt creator报错:Circular all - first dependency dropped
  3. NOIp 2014 #2 联合权值 Label:图论 !!!未AC
  4. 信号之函数的可重入性
  5. 使用print写文件
  6. 区块链开发公司:区块链技术如何改变个人数据安全
  7. Maven入门指南12:将项目发布到私服
  8. 天鹰优化的半监督拉普拉斯深度核极限学习机用于分类
  9. Android全局修改字体大小,Android 应用全局字体调节或禁止随系统字体大小更改
  10. 怎么将flac转换成mp3
  11. 【软件测试】测试人,我们35岁焦虑怎样破?
  12. 9.4 网易互娱客户端笔试
  13. 杜克大学陈怡然教授转载一篇谈ChatGPT为何无法出现在中国的文章,一针见血
  14. hankerrank 刷题二( Python 基础)
  15. A005 C++提高编程
  16. 带孩子们做环球旅行的读后感_适合夏天制作的简单小手工,带着孩子们做起来!...
  17. Spring启动过程详解
  18. c语言中其不同数据类型,C语言中不同类型数据间的转换
  19. 行列式的计算方法总结:
  20. 数字电路实验(十二)——CPU综合设计(8)

热门文章

  1. 数据的冗余和数据的完整性 1006
  2. python-发送短信验证码-功能的实现
  3. linux-进程杀死的操作
  4. javascript-dom-文档对象模型
  5. 需要写的一些pycoe
  6. #考研#计算机文化知识1(局域网及网络互联)
  7. 黄聪:VS2017调试时提示“运行时无法计算表达式的值”
  8. OO模式-Composite
  9. Tomcat5.5.9+JSP经典配置实例
  10. redhat python3.4安装步骤