gofiber: 模板:判断if条件
一,代码:
1,controller
func (dc *ArticleController) GetArticle(c *fiber.Ctx) error {
// 处理获取文章的逻辑
article := new(Article)
article.Id = 1
article.Title = "三国演义金圣叹批本"
article.Author = "罗贯中"
user:=c.Query("user")
myMap:=fiber.Map{}
myMap["Title"] = article.Title
myMap["Article"] = article
myMap["UserId"] = user
return c.Render("article/info", myMap)
}
2,view
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<title>中国古典文学</title>
</head>
<body style="padding: 0;margin: 0;">
<header style="width: 100%;text-align: center;background: #ff00ff;">
<span style="font-size:20px;">这里是header</span>
<span style="float:right;">
{{if eq .UserId ""}}
未登录
{{else if eq .UserId "123"}}
用户id:123,管理员,欢迎你!
{{else}}
用户id:{{.UserId}},欢迎你!
{{end}}
</span>
</header>
二,测试效果:
用户id为空
用户id不为空