Reapply "feat(controller): 完善基础控制器及默认页面渲染"
This reverts commit 1a3bd1a6b3.
This commit is contained in:
13
controller/admin/baseController.go
Normal file
13
controller/admin/baseController.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
type BaseController struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (con BaseController) Success(c *gin.Context) {
|
||||||
|
c.String(200, "成功")
|
||||||
|
}
|
||||||
|
func (con BaseController) Error(c *gin.Context) {
|
||||||
|
c.String(200, "失败")
|
||||||
|
}
|
||||||
@@ -3,10 +3,11 @@ package admin
|
|||||||
import "github.com/gin-gonic/gin"
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
type UserController struct {
|
type UserController struct {
|
||||||
|
BaseController
|
||||||
}
|
}
|
||||||
|
|
||||||
func (con UserController) Index(c *gin.Context) {
|
func (con UserController) Index(c *gin.Context) {
|
||||||
c.String(200, "管理员用户列表")
|
con.Success(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (con UserController) Show(c *gin.Context) {
|
func (con UserController) Show(c *gin.Context) {
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ type DefaultController struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (con DefaultController) Index(c *gin.Context) {
|
func (con DefaultController) Index(c *gin.Context) {
|
||||||
c.String(200, "首页")
|
c.HTML(200, "pages/index", gin.H{})
|
||||||
}
|
}
|
||||||
func (con DefaultController) Article(c *gin.Context) {
|
func (con DefaultController) Article(c *gin.Context) {
|
||||||
c.String(200, "文章详情")
|
c.String(200, "文章详情")
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
{{/* 定义index页面模板 */}}
|
|
||||||
{{define "pages/index"}}
|
{{define "pages/index"}}
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="zh">
|
<html lang="zh">
|
||||||
@@ -6,61 +5,50 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<title>{{.title}}</title>
|
<title>{{.title}}</title>
|
||||||
|
|
||||||
<!-- 组件特定样式块 -->
|
|
||||||
<link rel="stylesheet" href="/static/navbar.css">
|
<link rel="stylesheet" href="/static/navbar.css">
|
||||||
<link rel="stylesheet" href="/static/footer.css">
|
<link rel="stylesheet" href="/static/footer.css">
|
||||||
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
{{template "layout/navbar"}}
|
{{template "layout/navbar"}}
|
||||||
{{/* 基本数据输出示例 - 直接渲染标题变量 */}}
|
|
||||||
<h2>{{.title}}</h2>
|
<h2>{{.title}}</h2>
|
||||||
|
|
||||||
{{/* 模板变量定义示例 - 将标题赋值给局部变量t */}}
|
|
||||||
{{$t := .title}}
|
{{$t := .title}}
|
||||||
<h4>{{$t}}</h4>
|
<h4>{{$t}}</h4>
|
||||||
|
|
||||||
{{/* 条件判断示例 - 根据score值判断是否及格 */}}
|
{{if ge .score 60}}
|
||||||
{{if ge .score 60}} <!-- ge: 大于等于 -->
|
|
||||||
<p>及格</p>
|
<p>及格</p>
|
||||||
{{else}}
|
{{else}}
|
||||||
<p></p>
|
<p>不及格</p>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{/* 简单数组循环遍历示例 */}}
|
{{range $key, $value := .hobby}}
|
||||||
{{range $key, $value := .hobby}} <!-- 遍历爱好列表 -->
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>{{$key}}--{{$value}}</li> <!-- 输出索引和值 -->
|
<li>{{$key}}--{{$value}}</li>
|
||||||
</ul>
|
</ul>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{/* 结构体数组循环遍历示例 */}}
|
{{range $key, $value := .newsList}}
|
||||||
{{range $key, $value := .newsList}} <!-- 遍历文章列表 -->
|
|
||||||
<ul>
|
<ul>
|
||||||
<li>{{$key}}--{{$value.Title}}--{{$value.Content}}</li> <!-- 输出索引和文章属性 -->
|
<li>{{$key}}--{{$value.Title}}--{{$value.Content}}</li>
|
||||||
</ul>
|
</ul>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{/* 空数组处理示例 - 当newsList2为空时显示提示信息 */}}
|
|
||||||
{{range $key, $value := .newsList2}}
|
{{range $key, $value := .newsList2}}
|
||||||
<ul>
|
<ul>
|
||||||
<li>{{$key}}--{{$value.Title}}--{{$value.Content}}</li>
|
<li>{{$key}}--{{$value.Title}}--{{$value.Content}}</li>
|
||||||
</ul>
|
</ul>
|
||||||
{{else}}
|
{{else}}
|
||||||
<li>没有数据</li> <!-- 当数组为空时执行 -->
|
<li>没有数据</li>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{/* 结构体数据访问示例 - 使用with语句简化结构体属性访问 */}}
|
{{with .news}}
|
||||||
{{with .news}} <!-- 针对news结构体设置上下文 -->
|
{{.Title}}
|
||||||
{{.Title}} <!-- 直接访问Title属性,等同于.news.Title -->
|
{{.Content}}
|
||||||
{{.Content}} <!-- 直接访问Content属性,等同于.news.Content -->
|
|
||||||
{{end}}
|
{{end}}
|
||||||
<br>
|
<br>
|
||||||
|
|
||||||
{{/* 自定义模板函数使用示例 */}}
|
{{.data}}
|
||||||
{{.data}} <!-- 输出原始时间戳 -->
|
|
||||||
|
|
||||||
|
|
||||||
{{template "layout/footer"}}
|
{{template "layout/footer"}}
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user