最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Golang HTML 模板使用指南示例詳解

 更新時間:2025年01月14日 09:30:44   作者:老大白菜  
本文詳細介紹了Golang HTML模板的使用方法,包括基礎模板、高級模板、完整應用示例、CSS樣式、JavaScript交互等,文章強調了模板組織、代碼復用、語義化HTML、響應式設計、性能優(yōu)化等最佳實踐,感興趣的朋友跟隨小編一起看看吧

Golang HTML 模板使用指南

1. 基礎模板示例

1.1 簡單頁面模板

<!-- templates/layout.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{{.Title}}</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/static/css/style.css" rel="external nofollow" >
</head>
<body>
    <header>
        <h1>{{.Title}}</h1>
        <nav>
            <a href="/" rel="external nofollow" >首頁</a>
            <a href="/about" rel="external nofollow" >關于</a>
            <a href="/contact" rel="external nofollow" >聯(lián)系我們</a>
        </nav>
    </header>
    <main>
        {{template "content" .}}
    </main>
    <footer>
        <p>&copy; {{.Year}} 我的網站</p>
    </footer>
</body>
</html>
// main.go
package main
import (
    "html/template"
    "net/http"
    "time"
)
func main() {
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        tmpl := template.Must(template.ParseFiles("templates/layout.html"))
        data := struct {
            Title string
            Year  int
        }{
            Title: "我的網站",
            Year:  time.Now().Year(),
        }
        tmpl.Execute(w, data)
    })
    http.ListenAndServe(":8080", nil)
}

1.2 列表渲染

<!-- templates/products.html -->
{{define "content"}}
<div class="products">
    <h2>產品列表</h2>
    <div class="product-grid">
        {{range .Products}}
        <div class="product-card">
            <img src="{{.Image}}" alt="{{.Name}}">
            <h3>{{.Name}}</h3>
            <p>{{.Description}}</p>
            <div class="price">{{formatPrice .Price}}</div>
            {{if .InStock}}
                <button class="buy-btn">購買</button>
            {{else}}
                <button class="out-of-stock" disabled>缺貨</button>
            {{end}}
        </div>
        {{end}}
    </div>
</div>
{{end}}
type Product struct {
    Name        string
    Description string
    Price       float64
    Image       string
    InStock     bool
}
func productsHandler(w http.ResponseWriter, r *http.Request) {
    funcMap := template.FuncMap{
        "formatPrice": func(price float64) string {
            return fmt.Sprintf("¥%.2f", price)
        },
    }
    tmpl := template.New("layout.html").Funcs(funcMap)
    tmpl = template.Must(tmpl.ParseFiles(
        "templates/layout.html",
        "templates/products.html",
    ))
    data := struct {
        Title    string
        Year     int
        Products []Product
    }{
        Title: "產品列表",
        Year:  time.Now().Year(),
        Products: []Product{
            {
                Name:        "商品1",
                Description: "這是商品1的描述",
                Price:       99.99,
                Image:      "/static/images/product1.jpg",
                InStock:    true,
            },
            // 更多商品...
        },
    }
    tmpl.Execute(w, data)
}

2. 高級模板示例

2.1 嵌套模板

<!-- templates/components/header.html -->
{{define "header"}}
<header class="site-header">
    <div class="logo">
        <img src="/static/images/logo.png" alt="Logo">
    </div>
    <nav class="main-nav">
        <ul>
            {{range .NavItems}}
            <li class="{{if eq $.CurrentPage .Link}}active{{end}}">
                <a href="{{.Link}}" rel="external nofollow" >{{.Text}}</a>
            </li>
            {{end}}
        </ul>
    </nav>
    {{if .User}}
    <div class="user-menu">
        <span>歡迎, {{.User.Name}}</span>
        <a href="/logout" rel="external nofollow" >退出</a>
    </div>
    {{else}}
    <div class="auth-buttons">
        <a href="/login" rel="external nofollow"  class="btn btn-login">登錄</a>
        <a href="/register" rel="external nofollow"  class="btn btn-register">注冊</a>
    </div>
    {{end}}
</header>
{{end}}

2.2 表單處理

<!-- templates/form.html -->
{{define "content"}}
<div class="form-container">
    <h2>{{.FormTitle}}</h2>
    {{with .FormError}}
        <div class="error-message">{{.}}</div>
    {{end}}
    <form method="POST" action="{{.FormAction}}" enctype="multipart/form-data">
        {{range .Fields}}
        <div class="form-group">
            <label for="{{.ID}}">{{.Label}}{{if .Required}}*{{end}}</label>
            {{if eq .Type "text"}}
                <input type="text" id="{{.ID}}" name="{{.Name}}" 
                       value="{{.Value}}" {{if .Required}}required{{end}}
                       {{with .Pattern}}pattern="{{.}}"{{end}}>
            {{else if eq .Type "textarea"}}
                <textarea id="{{.ID}}" name="{{.Name}}" 
                         {{if .Required}}required{{end}}>{{.Value}}</textarea>
            {{else if eq .Type "select"}}
                <select id="{{.ID}}" name="{{.Name}}" 
                        {{if .Required}}required{{end}}>
                    {{range .Options}}
                    <option value="{{.Value}}" {{if eq .Value $.Selected}}selected{{end}}>
                        {{.Text}}
                    </option>
                    {{end}}
                </select>
            {{end}}
            {{with .Error}}
                <span class="field-error">{{.}}</span>
            {{end}}
        </div>
        {{end}}
        <div class="form-actions">
            <button type="submit" class="btn btn-primary">{{.SubmitText}}</button>
            <button type="reset" class="btn btn-secondary">重置</button>
        </div>
    </form>
</div>
{{end}}

2.3 分頁組件

<!-- templates/components/pagination.html -->
{{define "pagination"}}
<div class="pagination">
    {{if gt .TotalPages 1}}
        {{if gt .CurrentPage 1}}
            <a href="?page=1" rel="external nofollow"  class="page-link first">&laquo;</a>
            <a href="?page={{subtract .CurrentPage 1}}" rel="external nofollow"  class="page-link prev">&lsaquo;</a>
        {{end}}
        {{range $i := range (sequence .TotalPages)}}
            {{if and (ge $i (subtract $.CurrentPage 2)) (le $i (add $.CurrentPage 2))}}
                <a href="?page={{add $i 1}}" rel="external nofollow"  
                   class="page-link {{if eq $i (subtract $.CurrentPage 1)}}active{{end}}">
                    {{add $i 1}}
                </a>
            {{end}}
        {{end}}
        {{if lt .CurrentPage .TotalPages}}
            <a href="?page={{add .CurrentPage 1}}" rel="external nofollow"  class="page-link next">&rsaquo;</a>
            <a href="?page={{.TotalPages}}" rel="external nofollow"  class="page-link last">&raquo;</a>
        {{end}}
    {{end}}
</div>
{{end}}

3. 完整應用示例

3.1 博客系統(tǒng)模板

<!-- templates/blog/list.html -->
{{define "content"}}
<div class="blog-list">
    <div class="filters">
        <div class="search">
            <input type="text" placeholder="搜索文章..." 
                   value="{{.Query}}" id="searchInput">
        </div>
        <div class="categories">
            {{range .Categories}}
            <a href="/blog?category={{.Slug}}" rel="external nofollow"  
               class="category {{if eq $.CurrentCategory .Slug}}active{{end}}">
                {{.Name}} ({{.Count}})
            </a>
            {{end}}
        </div>
    </div>
    <div class="articles">
        {{range .Posts}}
        <article class="post-card">
            {{if .Image}}
            <div class="post-image">
                <img src="{{.Image}}" alt="{{.Title}}">
            </div>
            {{end}}
            <div class="post-content">
                <h2><a href="/blog/{{.Slug}}" rel="external nofollow" >{{.Title}}</a></h2>
                <div class="post-meta">
                    <span class="author">{{.Author}}</span>
                    <span class="date">{{formatDate .CreatedAt "2006-01-02"}}</span>
                    <span class="category">{{.Category}}</span>
                </div>
                <p class="excerpt">{{truncate .Content 200}}</p>
                <div class="tags">
                    {{range .Tags}}
                    <a href="/blog?tag={{.}}" rel="external nofollow"  class="tag">{{.}}</a>
                    {{end}}
                </div>
            </div>
        </article>
        {{else}}
        <div class="no-posts">
            暫無文章
        </div>
        {{end}}
    </div>
    {{template "pagination" .Pagination}}
</div>
{{end}}
type BlogData struct {
    Query           string
    CurrentCategory string
    Categories      []Category
    Posts           []Post
    Pagination      Pagination
}
type Category struct {
    Name  string
    Slug  string
    Count int
}
type Post struct {
    Title     string
    Slug      string
    Content   string
    Author    string
    Image     string
    Category  string
    Tags      []string
    CreatedAt time.Time
}
type Pagination struct {
    CurrentPage int
    TotalPages  int
    TotalItems  int
}
func blogHandler(w http.ResponseWriter, r *http.Request) {
    funcMap := template.FuncMap{
        "formatDate": func(t time.Time, layout string) string {
            return t.Format(layout)
        },
        "truncate": func(s string, l int) string {
            if len(s) <= l {
                return s
            }
            return s[:l] + "..."
        },
    }
    tmpl := template.New("layout.html").Funcs(funcMap)
    tmpl = template.Must(tmpl.ParseFiles(
        "templates/layout.html",
        "templates/blog/list.html",
        "templates/components/pagination.html",
    ))
    data := BlogData{
        Query:           r.URL.Query().Get("q"),
        CurrentCategory: r.URL.Query().Get("category"),
        Categories: []Category{
            {Name: "技術", Slug: "tech", Count: 10},
            {Name: "生活", Slug: "life", Count: 5},
        },
        Posts: []Post{
            {
                Title:     "示例文章",
                Slug:      "example-post",
                Content:   "這是一篇示例文章的內容...",
                Author:    "作者名",
                Category: "tech",
                Tags:     []string{"Go", "Web"},
                CreatedAt: time.Now(),
            },
        },
        Pagination: Pagination{
            CurrentPage: 1,
            TotalPages:  5,
            TotalItems:  45,
        },
    }
    tmpl.Execute(w, data)
}

4. CSS 樣式示例

/* static/css/style.css */
/* 基礎樣式 */
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    line-height: 1.6;
    margin: 0;
    padding: 0;
    color: #333;
}
/* 頭部樣式 */
.site-header {
    background: #fff;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    padding: 1rem;
}
.main-nav ul {
    list-style: none;
    display: flex;
    gap: 1rem;
}
/* 產品卡片樣式 */
.product-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
    gap: 2rem;
    padding: 2rem;
}
.product-card {
    border: 1px solid #eee;
    border-radius: 8px;
    overflow: hidden;
    transition: transform 0.2s;
}
.product-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
/* 表單樣式 */
.form-container {
    max-width: 600px;
    margin: 2rem auto;
    padding: 2rem;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.form-group {
    margin-bottom: 1.5rem;
}
/* 博客列表樣式 */
.blog-list {
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem;
}
.post-card {
    display: grid;
    grid-template-columns: 200px 1fr;
    gap: 1.5rem;
    margin-bottom: 2rem;
    padding: 1rem;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* 分頁樣式 */
.pagination {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    margin: 2rem 0;
}
.page-link {
    padding: 0.5rem 1rem;
    border: 1px solid #ddd;
    border-radius: 4px;
    color: #333;
    text-decoration: none;
}
.page-link.active {
    background: #007bff;
    color: #fff;
    border-color: #007bff;
}

5. JavaScript 交互示例

// static/js/main.js
document.addEventListener('DOMContentLoaded', function() {
    // 搜索功能
    const searchInput = document.getElementById('searchInput');
    if (searchInput) {
        searchInput.addEventListener('input', debounce(function(e) {
            const query = e.target.value;
            window.location.href = `/blog?q=${encodeURIComponent(query)}`;
        }, 500));
    }
    // 表單驗證
    const forms = document.querySelectorAll('form');
    forms.forEach(form => {
        form.addEventListener('submit', function(e) {
            const requiredFields = form.querySelectorAll('[required]');
            let valid = true;
            requiredFields.forEach(field => {
                if (!field.value.trim()) {
                    valid = false;
                    field.classList.add('error');
                } else {
                    field.classList.remove('error');
                }
            });
            if (!valid) {
                e.preventDefault();
                alert('請?zhí)顚懰斜靥钭侄?);
            }
        });
    });
});
// 工具函數
function debounce(func, wait) {
    let timeout;
    return function executedFunction(...args) {
        const later = () => {
            clearTimeout(timeout);
            func(...args);
        };
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
    };
}

總結

模板組織

  • 使用嵌套模板實現代碼復用
  • 保持模板結構清晰
  • 合理使用模板函數

最佳實踐

  • 使用語義化 HTML
  • 保持 CSS 模塊化
  • 實現響應式設計
  • 添加適當的交互效果

性能優(yōu)化

  • 緩存編譯后的模板
  • 壓縮靜態(tài)資源
  • 使用 CDN 加速
  • 實現懶加載

到此這篇關于Golang HTML 模板使用指南的文章就介紹到這了,更多相關Golang HTML 模板內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Windows環(huán)境下vscode-go安裝筆記(不支持32位)

    Windows環(huán)境下vscode-go安裝筆記(不支持32位)

    這篇文章主要介紹了Windows環(huán)境下vscode-go安裝筆記(不支持32位),需要的朋友可以參考下
    2017-02-02
  • 詳解Golang實現http重定向https的方式

    詳解Golang實現http重定向https的方式

    這篇文章主要介紹了詳解Golang實現http重定向https的方式,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Go高級特性探究之處理1分鐘百萬請求詳解

    Go高級特性探究之處理1分鐘百萬請求詳解

    對于大型的互聯(lián)網應用程序,如電商平臺、社交網絡、金融交易平臺等,每秒鐘都會收到大量的請求,那么Go是如何處理這些百萬請求的呢,下面就來和大家詳細講講
    2023-06-06
  • 淺析GO語言的垃圾回收機制

    淺析GO語言的垃圾回收機制

    今天我們來聊聊golang是如何進行垃圾回收的,我們知道,目前各語言進行垃圾回收的方法有很多,如引用計數、標記清除、分代回收、三色標記等,各種方式都有其特點,文中介紹的非常詳細,感興趣的小伙伴跟著小編一起學習吧
    2023-07-07
  • GO語言入門學習之基本數據類型字符串

    GO語言入門學習之基本數據類型字符串

    字符串在Go語言中以原生數據類型出現,使用字符串就像使用其他原生數據類型(int、bool、float32、float64 等)一樣,下面這篇文章主要給大家介紹了關于GO語言入門學習之基本數據類型字符串的相關資料,需要的朋友可以參考下
    2022-04-04
  • Golang結構化日志包log/slog的使用詳解

    Golang結構化日志包log/slog的使用詳解

    官方提供的用于打印日志的包是標準庫中的 log 包,該包雖然被廣泛使用,但是缺點也很多,所以Go 1.21新增的 log/slog 完美解決了以上問題,下面我們就來看看log/slog包的使用吧
    2023-09-09
  • GO 語言運行環(huán)境的基礎知識

    GO 語言運行環(huán)境的基礎知識

    這篇文章主要介紹了GO 語言運行環(huán)境的基礎知識的相關資料,需要的朋友可以參考下
    2022-09-09
  • 詳解Golang中Context的原理和使用技巧

    詳解Golang中Context的原理和使用技巧

    Golang?的?Context?包,中文可以稱之為“上下文”,是用來在?goroutine?協(xié)程之間進行上下文信息傳遞的,這些上下文信息包括?kv?數據、取消信號、超時時間、截止時間等。本文主要介紹了Context的原理和使用技巧,希望對大家有所幫助
    2022-11-11
  • Golang IPv4 字符串與整數互轉方式

    Golang IPv4 字符串與整數互轉方式

    這篇文章主要介紹了Golang IPv4 字符串與整數互轉方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 詳解Go語言中上下文context的理解與使用

    詳解Go語言中上下文context的理解與使用

    在Go的日常開發(fā)中,Context上下文對象無處不在,這篇文章小編就來帶大家深入了解一下上下文context的理解與使用,文中的示例代碼講解詳細,需要的可以參考下
    2023-10-10

最新評論

庄河市| 扬中市| 青田县| 赤壁市| 嘉禾县| 新沂市| 新平| 嘉峪关市| 绥中县| 昭苏县| 洞口县| 富民县| 福清市| 自治县| 陆河县| 泸州市| 阜新| 罗源县| 永城市| 青神县| 海口市| 彭山县| 汉源县| 延安市| 怀来县| 丽江市| 丹凤县| 定陶县| 伊金霍洛旗| 瑞丽市| 钟山县| 穆棱市| 岗巴县| 永川市| 左云县| 乌鲁木齐市| 怀宁县| 陆河县| 喀什市| 新干县| 康马县|