博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
go语言 echo框架_如何在Go Echo Web框架中设置嵌套HTML模板
阅读量:2517 次
发布时间:2019-05-11

本文共 6024 字,大约阅读时间需要 20 分钟。

go语言 echo框架

by Ying Kit Yuen

英杰苑

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

is a lightweight but complete web framework in 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 package for the purpose of simplicity. By the end of this article, we will have a nested template project setup.

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

If you already have an idea on how works, jump to the .

如果您已经对工作原理有所了解,请跳至 。

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

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

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

完整的项目代码托管在 。 首先,我们将在此处创建项目文件夹: $ 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 official site and create the main.go.

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

main.go

main.go

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

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

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

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

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

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

Start the application by typing go run main.go and then visit through the browser or the curl command.

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

返回一个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 code in main.go.

构建RESTful API时,客户端更有可能希望接收JSON响应而不是字符串。 让我们在main.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. has a few more convenient ways to return JSON and HTML. For details please refer to the .

以上只是两个简单的示例。 有一些更方便的方法来返回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 server to render the html response, and this is configured in the main() flow.

在这个main.go中 ,我们定义一个名为TemplateRegistry的类型并实现Renderer接口。 Renderer是包装Render()函数的简单接口。 在TemplateRegistry实例内部,它具有一个template字段,其中包含服务器呈现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 created by .

下面的代码就是基于这一所创造 。

main.go

main.go

We add a new route /about which is handled by an AboutHandler. As you can see from the above lines , 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处理 从上面的第行可以看到, 模板数组包含用于不同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 standard library in . With proper setup, we could develop a more customized and convenient pattern for or even make it work with any other template engines.

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

The complete example can be found on .

完整的示例可以在上 。

— Originally posted on .

—最初发布在 。

翻译自:

go语言 echo框架

转载地址:http://obewd.baihongyu.com/

你可能感兴趣的文章
stack
查看>>
js 压缩
查看>>
oracle数据库----笔记1---PL/SQL基础5---子程序
查看>>
OpenCV学习笔记(二)——OpenCV环境变量配置
查看>>
二叉树遍历(flist)(已知中序和按层遍历,求先序 )
查看>>
自然语言处理第一讲:简介和概述
查看>>
儿童摄影欧美浓郁温暖效果步骤
查看>>
CF 910 C. Minimum Sum
查看>>
jquey :eq(1)
查看>>
buffer busy waits等待事件案例-vage
查看>>
5. support vector machine
查看>>
hdu 3345
查看>>
原型模式
查看>>
航空例行天气预报解析 metaf2xml
查看>>
货车运输
查看>>
linux实现多台服务器时时同步文件,推荐lsyncd
查看>>
覆盖墙壁
查看>>
入门级(python)
查看>>
Delphi 解析HTML
查看>>
讲讲最近自己的学习,谈谈未来的想法
查看>>