🎯 指定内容(one 函数)

最后更新:2026-11-04

在模板任意位置,通过 one 函数从数据库取单条内容。返回对象,字段同列表页(.Title、.Content、.Summary、.Image 等)。

⚡ 基本语法

<!-- 单条内容:type + id -->
{{with one "type=news&id=1"}}
<h1>{{.Title}}</h1>
<div>{{.Content | safeHTML}}</div>
{{end}}

with 关键字:进入对象上下文,避免每次写变量前缀。找不到内容时整段不输出。

📋 参数说明

参数 说明 示例
type 内容类型:news / product / case / page type=news
id 内容 ID(必填) id=12
site 站点 ID(多语言,可选) site=2

💻 常用场景

<!-- 首页取「公司动态」栏目置顶新闻 -->
{{with one "type=news&id=10"}}
  <h3>{{.Title}}</h3>
  <p>{{.Summary}}</p>
  <a href="{{.URL}}">查看详情 →</a>
{{end}}
<!-- 取指定产品(首页推荐主推) -->
{{with one "type=product&id=5"}}
  <div class="hero-product">
    <img src="{{.Thumbnail}}" alt="{{.Title}}">
    <h2>{{.Title}}</h2>
    <p>{{.Description}}</p>
  </div>
{{end}}

html <!-- 取关于我们单页 --> {{with one "type=page&id=1"}} <h1>{{.Title}}</h1> <div>{{.Content | safeHTML}}</div> {{end}} ← 内容详情 多图遍历 →

本网站使用 Cookie 以保障基本功能与改善体验。详情见隐私政策。