当前位置: 首页 > news >正文

网站设计的公司排名室内装修公司招聘信息

网站设计的公司排名,室内装修公司招聘信息,如何把网站推广出去,牡丹江做网站建设HTML渲染 【示例1】 首先定义一个存放模板文件的 templates文件夹#xff0c;然后在其内部按照业务分别定义一个 posts 文件夹和一个 users 文件夹。 posts/index.tmpl {{define posts/index.tmpl}} !DOCTYPE html html langen然后在其内部按照业务分别定义一个 posts 文件夹和一个 users 文件夹。 posts/index.tmpl {{define posts/index.tmpl}} !DOCTYPE html html langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0meta http-equivX-UA-Compatible contentieedgetitleposts/index/title /head body{{.title}} /body /html {{end}}users/index.tmpl {{define users/index.tmpl}} !DOCTYPE html html langen headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0meta http-equivX-UA-Compatible contentieedgetitleusers/index/title /head body{{.title}} /body /html {{end}}main.go Gin框架中使用 LoadHTMLGlob() 或者 LoadHTMLFiles() 方法进行HTML模板渲染。 package mainimport (github.com/gin-gonic/ginnet/http )func main() {r : gin.Default()// r.LoadHTMLFiles(templates/index.tmpl, templates/users/index.tmpl) // 模板解析r.LoadHTMLGlob(templates/**/*) // 从templates4目录及其所有子目录中加载所有的文件r.GET(/posts/index, func(c *gin.Context) {// HTTP请求c.HTML(http.StatusOK, posts/index.tmpl, gin.H{ // 模板渲染title: posts/index.tmpl,})})r.GET(/users/index, func(c *gin.Context) {// HTTP请求c.HTML(http.StatusOK, users/index.tmpl, gin.H{ // 模板渲染title: users/index.tmpl,})})r.Run(:9090) // 启动server } 效果 【示例2】 main.go package mainimport (github.com/gin-gonic/ginnet/http )func main() {r : gin.Default()r.LoadHTMLFiles(./index.tmpl) // 模板解析r.GET(/index, func(c *gin.Context) {// HTTP请求c.HTML(http.StatusOK, index.tmpl, gin.H{ // 模板渲染title: liwenzhou.com,})})r.Run(:9090) // 启动server } index.tmpl !DOCTYPE html html langen headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0meta http-equivX-UA-Compatible contentieedgetitleposts/index/title /head body {{.title}} /body /html效果 自定义模板函数 定义一个不转义相应内容的 safe 模板函数 main.go package mainimport (github.com/gin-gonic/ginhtml/templatenet/http )func main() {r : gin.Default()// gin框架中给模板添加自定义函数r.SetFuncMap(template.FuncMap{safe: func(str string) template.HTML {return template.HTML(str)},})r.LoadHTMLGlob(templates/**/*) // 从templates4目录及其所有子目录中加载所有的文件r.GET(/users/index, func(c *gin.Context) {// HTTP请求c.HTML(http.StatusOK, users/index.tmpl, gin.H{ // 模板渲染title: a hrefhttps://liwenzhou.com李文周的博客/a,})})r.Run(:9090) // 启动server } users/index.tmpl {{define users/index.tmpl}}!DOCTYPE htmlhtml langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0meta http-equivX-UA-Compatible contentieedgetitleusers/index/title/headbody{{.title | safe}}/body/html {{end}}效果 静态文件处理 当渲染的HTML文件中引用了静态文件时只需要按照以下方式在渲染页面前调用 gin.Static 方法即可。 main.go package mainimport (github.com/gin-gonic/ginhtml/templatenet/http )// 静态文件: html页面上用到的样式文件 。css js文件 图片 func main() {r : gin.Default()// 加载静态文件r.Static(/xxx, ./statics)// gin框架中给模板添加自定义函数r.SetFuncMap(template.FuncMap{safe: func(str string) template.HTML {return template.HTML(str)},})r.LoadHTMLGlob(templates/**/*) // 从templates4目录及其所有子目录中加载所有的文件r.GET(/users/index, func(c *gin.Context) {// HTTP请求c.HTML(http.StatusOK, users/index.tmpl, gin.H{ // 模板渲染title: a hrefhttps://liwenzhou.com李文周的博客/a,})})r.Run(:9090) // 启动server }users/index.tmpl {{define users/index.tmpl}}!DOCTYPE htmlhtml langenheadmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0meta http-equivX-UA-Compatible contentieedgelink relstylesheet href/xxx/index.csstitleusers/index/title/headbody{{.title | safe}}/body/html {{end}}index.css body {background-color: cadetblue; }效果 使用模板继承 Gin框架默认都是使用单模板如果需要使用 block template 功能可以通过 github.com/gin-contrib/multitemplate 库实现具体示例如下 首先假设项目目录下的templates文件夹下有以下模板文件其中 home.tmpl 和 index.tmpl 继承了 base.tmpl templates ├── includes │ ├── home.tmpl │ └── index.tmpl ├── layouts │ └── base.tmpl └── scripts.tmpl定义一个 loadTemplates 函数 func loadTemplates(templatesDir string) multitemplate.Renderer {r : multitemplate.NewRenderer()layouts, err : filepath.Glob(templatesDir /layouts/*.tmpl)if err ! nil {panic(err.Error())}includes, err : filepath.Glob(templatesDir /includes/*.tmpl)if err ! nil {panic(err.Error())}// 为layouts/和includes/目录生成 templates mapfor _, include : range includes {layoutCopy : make([]string, len(layouts))copy(layoutCopy, layouts)files : append(layoutCopy, include)r.AddFromFiles(filepath.Base(include), files...)}return r }main 函数 func indexFunc(c *gin.Context){c.HTML(http.StatusOK, index.tmpl, nil) }func homeFunc(c *gin.Context){c.HTML(http.StatusOK, home.tmpl, nil) }func main(){r : gin.Default()r.HTMLRender loadTemplates(./templates)r.GET(/index, indexFunc)r.GET(/home, homeFunc)r.Run() }补充文件路径处理 关于模板文件和静态文件的路径需要根据公司/项目的要求进行设置。可以使用下面的函数获取当前执行程序的路径。 func getCurrentPath() string {if ex, err : os.Executable(); err nil {return filepath.Dir(ex)}return ./ }JSON渲染 方法1使用map main.go1 map package mainimport (github.com/gin-gonic/ginnet/http )func main() {r : gin.Default()r.GET(/json, func(c *gin.Context) {data : map[string]interface{}{name: 小王子,message: hello world!,age: 18,}c.JSON(http.StatusOK, data)})r.Run(:9090) }main.go2gin.H package mainimport (github.com/gin-gonic/ginnet/http )func main() {r : gin.Default()r.GET(/json, func(c *gin.Context) {data : gin.H{name: 小王子, message: hello world!, age: 18}c.JSON(http.StatusOK, data)})r.Run(:9090) }效果 方法2: 结构体 main,go package mainimport (github.com/gin-gonic/ginnet/http )func main() {r : gin.Default()type msg struct {Name stringMessage stringAge int}r.GET(/another_json, func(c *gin.Context) {data : msg{小王子,Hello golang!,18,}c.JSON(http.StatusOK, data)})r.Run(:9090) }效果 使用tag package mainimport (github.com/gin-gonic/ginnet/http )func main() {r : gin.Default()type msg struct {// 灵活使用tag来对结构体字段做定制化操作Name string json:nameMessage stringAge int}r.GET(/another_json, func(c *gin.Context) {data : msg{小王子,Hello golang!,18,}c.JSON(http.StatusOK, data)})r.Run(:9090) }XML渲染 注意需要使用具名的结构体类型。 func main() {r : gin.Default()// gin.H 是map[string]interface{}的缩写r.GET(/someXML, func(c *gin.Context) {// 方式一自己拼接JSONc.XML(http.StatusOK, gin.H{message: Hello world!})})r.GET(/moreXML, func(c *gin.Context) {// 方法二使用结构体type MessageRecord struct {Name stringMessage stringAge int}var msg MessageRecordmsg.Name 小王子msg.Message Hello world!msg.Age 18c.XML(http.StatusOK, msg)})r.Run(:8080) }YMAL渲染 r.GET(/someYAML, func(c *gin.Context) {c.YAML(http.StatusOK, gin.H{message: ok, status: http.StatusOK}) })protobuf渲染 r.GET(/someProtoBuf, func(c *gin.Context) {reps : []int64{int64(1), int64(2)}label : test// protobuf 的具体定义写在 testdata/protoexample 文件中。data : protoexample.Test{Label: label,Reps: reps,}// 请注意数据在响应中变为二进制数据// 将输出被 protoexample.Test protobuf 序列化了的数据c.ProtoBuf(http.StatusOK, data) })
http://www.w-s-a.com/news/427884/

相关文章:

  • 在线教育网站开发网站推广常用方法包括
  • 东莞高端品牌网站建设软件开发模型及特点
  • 个人网站的设计与实现的主要内容网站开发公司架构
  • 浏览器收录网站什么是新媒体营销
  • 上海营销网站建设公司下面哪个不是网页制作工具
  • 有哪些网站可以做设计比赛苏州设计公司排名前十
  • 公益网站建设需求车陂手机网站开发
  • 高端网站建设专业营销团队宁德网站建设51yunsou
  • 网站如何做cdn购物网站建设app开发
  • 简单的手机网站模板好看大方的企业网站源码.net
  • 沈阳住房和城乡建设厅网站网站个人备案做论坛
  • 企业建网站的目的开家网站建设培训班
  • 做怎么网站网站优化和推广
  • 建站工具 风铃网站每年空间域名费用及维护费
  • 网站开发工具 知乎工业软件开发技术就业前景
  • 永济微网站建设费用新手如何自学编程
  • 在本地怎么做网站深圳保障房申请条件2022
  • 广州天河区网站建设公司东莞网络游戏制作开发
  • 哪个网站做免费小程序rio门户网站的制作
  • 短网站生成查询网站所有关键词排名
  • 阿里云购买网站登录技术服务外包公司
  • 淘宝单页面网站手机制作游戏的软件
  • 汉中市网站建设wordpress编辑器好麻烦
  • 织梦做的网站快照被攻击在线看crm系统
  • 青岛物流公司网站建设网站建设提议
  • 企业网站建设高端品牌宿州注册公司多少钱
  • 个人微信公众号怎么做微网站吗湛江网站制作方案
  • 学校网站改版南京展厅设计装修
  • 手机网站有免费做的吗建设银行网站不能登录
  • 树莓派做影视网站网站建设企业 熊账号