tmp和html在Go中有什么区别?

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

What is the difference between tmp and html in Go?

问题

我最近正在学习Golang,并尝试使用Go作为后端创建一个网站。
我已经使用一个叫做Martini的框架成功完成了,但我想不使用框架来实现。

有人可以告诉我html和tmpl之间的区别吗?因为我想调用一个加载数据库行的表格的页面,我猜我首先要了解它们的区别。

这是我尝试的代码:

server.go:

package main

import (
	"io/ioutil"
	"net/http"
	"html/template"
)

func main(){

	http.HandleFunc("/index/", viewIndex)
	http.ListenAndServe(":8080", nil)

}

func viewIndex(w http.ResponseWriter, r *http.Request){

	t, _ := template.ParseFiles("index.html")

}

我不知道结构是什么,但我把index.html放在/templates/index.html中:

内容为Hello World

提前谢谢。

英文:

Im recently learning Golang and Im trying to create a website with Go as backend.
I done it correctly with a framework called Martini but I want to do it without frameworks.

Can anyone tell me whats the difference between html and tmpl? Because I want to call a page that loads a table with DB rows and I guess that first I have to understand what is the difference.

This is what I tried:

server.go:

package main

import (
	"io/ioutil"
	"net/http"
	"html/template"
)

func main(){

	http.HandleFunc("/index/"), viewIndex)
	http.ListenAndServe(":8080", nil)

}

func viewIndex(w http.ResponseWriter, r *http.Request){

	t, _ := template.ParseFiles("index.html")

}

I dont know what is the structure but I put the index.html on : /templates/index.html:

Contains Hello World

Thank You in Advance.

答案1

得分: 2

没有。将文件命名为index.htmlindex.tmpl取决于作者。我个人更喜欢.tmpl,因为这些文件包含的不仅仅是HTML。

一些特定的包(比如martini-render)可能只会查找特定的文件扩展名,但几乎所有的包都是可配置的。

如果你刚刚开始学习,我建议阅读http://jan.newmarch.name/golang/template/chapter-template.html

英文:

None. Calling a file index.html or index.tmpl is up to the author. I personally prefer .tmpl as the files contain more than just HTML.

Some specific packages (like martini-render) might look only for certain file extensions, but nearly all should be configurable.

If you are just starting out I suggest reading http://jan.newmarch.name/golang/template/chapter-template.html

huangapple
  • 本文由 发表于 2014年7月8日 15:31:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/24626093.html
匿名

发表评论

匿名网友

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

确定