全局变量字典

ChanCMS 文档 · 4181 字

所有页面都能直接用的变量,由 init.js 注入。记住这张表,你就不用每次去翻代码确认「导航怎么取」「站点名是哪个字段」。

一、总表

变量来源形状典型用法
siteCommon.site()对象{{site.name}}
navtree(category)树形数组{{each nav}}
friendlinkCommon.friendLink数组{{each friendlink}}
fragCommon.frag对象(按 mark 索引){{@ frag.xxx.content}}
tagCommon.tag数组{{each tag}}
templatesite.template字符串{{template}}
domainsite.domain字符串(已规范化){{domain}}
static_url派生/view/{主题}/static/{{static_url}}css/style.css
appName / versionconfig字符串页脚版权
absUrl(u)init 注入函数{{absUrl(img)}}
cateUrl(c)init 注入函数{{cateUrl(item)}}
jsStr(v)init 注入函数{{@ jsStr(x)}}
renderContenthelper函数正文按 markdown/html 渲染
注意:category 不在模板作用域里。init.js 把它挂在 req.app 上供控制器用,并从 locals 里剔除了——这是为了避免模板里 category / cate / nav 三个名字混淆。取栏目用 cate(当前页)或 nav(导航树)。

二、site:站点配置

Common.site() 显式 select 的字段:

name, logo, domain, email, tel, address, icp, copyright, code,
status, close_tips, json, title, keywords, description,
template, uploadWay, ext_data
字段说明
name站点名称
logoLogo 图片路径,可直接当 <img src>
domain域名,已规范化为「带协议、无尾斜杠」
title / keywords / description全站 SEO 兜底值
tel / email / address / icp联系方式与备案号
status0 = 站点关闭(全站 503)
close_tips关闭时的提示文案
template当前主题名
uploadWay1 本地 / 2 七牛 / 3 阿里 OSS / 4 腾讯 COS
ext_data扩展字段袋,不提升到顶层
拼域名时不要再加协议。site.domain 已经是 https://example.com 形式。写 http://{{site.domain}} 会产出 http://https://example.com。要拼绝对地址用 {{absUrl(path)}}。

ext_data 的读法

{{site.ext_data.logo_text}}
{{site.ext_data.qr}}
{{site.ext_data.footer_about}}

本站把页头 Logo 文字、页脚简介、二维码、页脚三栏链接都放在这里,对应「全站配置」模型(model_code='site_global')。

三、nav:导航树

nav 是 tree(category) 的产物,每个节点都有 children:

{
  id, pid, name, slug, path, type, url, target,
  status, orderBy, content_type, listView, articleView,
  seo: { title, keywords, description },
  hasSeo, aigeo, model_code,
  children: [ …同结构 ]
}
字段说明
type0 列表 / 1 单页
url非空即外链,优先输出
target1 = 新窗口
status0 显示 / 1 不进导航(页面本身照样 200)
content_type内容域:article / product / page / book / special
children子栏目数组

渲染两级导航

<nav>
{{each nav item}}
  <a href="{{cateUrl(item)}}"{{if item.target == 1}} target="_blank" rel="noopener noreferrer"{{/if}}>{{item.name}}</a>
  {{if item.children && item.children.length}}
  <div class="dropdown">
    {{each item.children child}}
      <a href="{{cateUrl(child)}}">{{child.name}}</a>
    {{/each}}
  </div>
  {{/if}}
{{/each}}
</nav>
导航只能由栏目树渲染。不允许建一个「导航」模型字段手填链接。理由:导航本质是栏目树的投影,另存一份必然与栏目脱同步;而且 json 字段在后台只是裸文本框,运营改不动。改导航 = 去「栏目管理」改 pid / orderBy / name / url。

导航高亮

{{if cate && (cate.id == item.id || cate.pid == item.id)}} class="active"{{/if}}

判断「当前栏目是它本身,或它的子栏目」。不要用 slug 硬编码({{if cate.slug == 'news'}}),栏目改名就失效。

四、frag:内容碎片

碎片是「不归属任何文章的小段内容」,比如页脚备案文案、侧边栏广告位。形状是:

frag = {
  "footer_note": { content: "<p>...</p>", mid: 12, ext_data: {...} },
  "sidebar_ad":  { content: "...", mid: 0, ext_data: null }
}

取值:

{{@ frag.footer_note.content}}
{{frag.footer_note.ext_data.xxx}}

判断存在再输出:

{{if frag && frag.footer_note}}{{@ frag.footer_note.content}}{{/if}}

五、tag / friendlink

{{each tag t}}
  <a href="/tags/{{t.path}}/tag.html">{{t.name}} ({{t.count}})</a>
{{/each}}

{{each friendlink f}}
  <a href="{{f.link}}" target="_blank" rel="noopener">{{f.title}}</a>
{{/each}}

友链的扩展字段是扁平的(不套 ext_data),直接用 {{f.xxx}}。

六、三个辅助函数

cateUrl(c)

栏目 → 链接,全站唯一口径。详见「路由契约与 URL 规则」。

absUrl(u)

<meta property="og:image" content="{{absUrl(article.img)}}">

相对路径补成绝对地址。入参本身是绝对地址时原样返回。

jsStr(v)

<script>
  window.__DATA__ = {{@ jsStr(JSON.stringify({ id: article.id, title: article.title }))}};
</script>

返回带引号的 JSON 字符串字面量,引号、反斜杠、换行都已转义。

七、一个完整的 head 片段

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="{{static_url}}css/style.css">

{{set _c = (typeof cate !== 'undefined' && cate) ? cate : null}}
{{set _a = (typeof article !== 'undefined' && article) ? article : null}}
{{set _ogTitle = (_c && (_c.type == 1 || !_a || !_a.id))
  ? (_c.seoTitle || _c.name + ' - ' + site.name)
  : ((_a && _a.id) ? (_a.seoTitle || _a.title) : site.title)}}

<meta property="og:site_name" content="{{site.name}}">
<meta property="og:title" content="{{_ogTitle}}">
<meta property="og:image" content="{{absUrl(site.logo)}}">
{{include './aigeo.html'}}

这段的三层兜底逻辑值得记住:有栏目取栏目 → 有文章取文章 → 都没有取站点。所有页面共用同一个 head,靠这个三元表达式自适应。

微信二维码

扫码添加微信咨询

微信号:yanyutao2014

在线留言

留下您的需求,我们会在 1 个工作日内联系您。