Files
GoCode/templates/pages/index.html

68 lines
2.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{{/* 定义index页面模板 */}}
{{define "pages/index"}}
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{.title}}</title>
<!-- 组件特定样式块 -->
<link rel="stylesheet" href="/static/navbar.css">
<link rel="stylesheet" href="/static/footer.css">
</head>
<body>
{{template "layout/navbar"}}
{{/* 基本数据输出示例 - 直接渲染标题变量 */}}
<h2>{{.title}}</h2>
{{/* 模板变量定义示例 - 将标题赋值给局部变量t */}}
{{$t := .title}}
<h4>{{$t}}</h4>
{{/* 条件判断示例 - 根据score值判断是否及格 */}}
{{if ge .score 60}} <!-- ge: 大于等于 -->
<p>及格</p>
{{else}}
<p></p>
{{end}}
{{/* 简单数组循环遍历示例 */}}
{{range $key, $value := .hobby}} <!-- 遍历爱好列表 -->
<ul>
<li>{{$key}}--{{$value}}</li> <!-- 输出索引和值 -->
</ul>
{{end}}
{{/* 结构体数组循环遍历示例 */}}
{{range $key, $value := .newsList}} <!-- 遍历文章列表 -->
<ul>
<li>{{$key}}--{{$value.Title}}--{{$value.Content}}</li> <!-- 输出索引和文章属性 -->
</ul>
{{end}}
{{/* 空数组处理示例 - 当newsList2为空时显示提示信息 */}}
{{range $key, $value := .newsList2}}
<ul>
<li>{{$key}}--{{$value.Title}}--{{$value.Content}}</li>
</ul>
{{else}}
<li>没有数据</li> <!-- 当数组为空时执行 -->
{{end}}
{{/* 结构体数据访问示例 - 使用with语句简化结构体属性访问 */}}
{{with .news}} <!-- 针对news结构体设置上下文 -->
{{.Title}} <!-- 直接访问Title属性等同于.news.Title -->
{{.Content}} <!-- 直接访问Content属性等同于.news.Content -->
{{end}}
<br>
{{/* 自定义模板函数使用示例 */}}
{{.data}} <!-- 输出原始时间戳 -->
{{ template "layout/footer"}}
</body>
</html>
{{end}}