各位好,我最近要写个web服务,由于并不复杂,就直接通过模板来生成前段数据了。
我使用的是gin,模板目前有3个文件,分别是
base.html (基础模板)
index.html (首页)
login.html (登陆页面)
代码稍后贴出。目前的问题是,在base.html作为基础模板,定义了一些block块,在index和login用。全部配置好后发现,login.html李block content的内容覆盖了index.html中block content的内容。我一直找不到问题出在哪里。所以特莱求助,以下是三个html的代码:
- base.html
<!DOCTYPE html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="/static/css/base.css?v=0.1"> {{block "head" .}}{{end}} </head> <body> <header class="base-header"> <div class="logo">Plutus</div> </header> <div class="menu"> <ul> <li>a</li> <li>b</li> </ul> </div> <div class="content"> {{ block "content" . }} {{end}} </div> </body> {{block "js" .}}{{end}} </html>
- index.html
{{define "index/index.html"}} {{template "base.html" .}} {{end}} {{define "content"}} index {{end}}
- login.html
{{define "login/login.html"}} {{template "base.html" .}} {{end}} {{define "head"}} <link rel="stylesheet" href="/static/css/login.css?v=0.1"> {{end}} {{define "content"}} <form class="login-area" method="post" action="/login"> <input class="input" type="text" name="username" placeholder="用户名" /> <input class="input" type="password" name="password" placeholder="密码" /> <input class="login-action" type="submit" value="登陆"/> </form> {{end}}
以上是三个模板的代码。最后再总结一下现象就是: 进入index后看到的是login的登陆表单,而index李的 “index”字符串则看不到。求各位解惑。感谢。
哦 后端 template设置方法是: router.LoadHTMLGlob("template/*/")
感谢各位
多模板布局渲染的可以用这个库:github.com/gin-contrib/multitemplate,里面有例子。
你这个应该是一次load,定义的conent被覆盖了。