本文介绍RichCMS的页面自带变量,在html模板上,页面变量模板函数会配合使用。

文章《RichCMS模板使用教程一:基础篇》中的内容,都是页面变量,比如可以使用以下形式显示网站的title信息:

<title><% .config.Title %></title>

一、栏目页(文章翻页列表页)

自带变量:

  • category

比如可以通过一下方式,显示栏目的TDK信息。

<title><% if .category.Title%><% .category.Title%><%else%><% .category.CateName%><%end%></title>
<meta name="keywords" content="<% .category.Keywords %>" />
<meta name="description" content="<% .category.Description %>" />

代码说明:

  1. 如果设置有此栏目的 title信息,则显示 title,否者,显示栏目的中文名。
  2. 显示栏目的Keywords
  3. 显示栏目的Description

栏目页结构:

type Category struct {
	CateId       int               //栏目id
	CateName     string            //中文名称
	CateEName    string            //英文名
	ParentId     int               //上级id
	RootId       int               //根id
	Dept         int               //层级
	CategoryType int               //类型
	RedirectUrl  string            //跳转地地址
	State        int               //状态
	Title        string            //SEO 标题
	Keywords     string            //SEO keywords
	Description  string            //SEO说明
	Link         string            //链接地址
}


二、文章详情页

自带变量

  • artiche

例子1:

<title><%if .article.SeoTitle%><%.article.SeoTitle%><%else%><%.article.Title%><%end%></title>
<meta name="keywords" content="<% .article.Keywords %>" />
<meta name="description" content="<% .article.Description %>" />

代码说明:

  1. 如果设置有此栏目的 SeoTitle 信息,则显示 SeoTitle,否则显示文章标题
  2. 显示栏目的Keywords
  3. 显示栏目的Description

例子2:

<% $cate:=fn_category .article.CateId %>
<div class="details-wrapper">
  <div class="post-info"></div><span class="post-info">作者:<% .article.Author %></span>
  <div class="post-info">|</div><span class="post-info">时间:<% fn_datetime_format .article.Updated%></span>
  <div class="post-info">|</div><a href="<% $cate.Link %>" class="post-info when-link">栏目:<% $cate.CateName%></a>
  <div class="post-info">|</div><a href="<% .article.MonthLink %>" class="post-info when-link">归档:<% .article.Month %></a>
 </div>

代码说明:

  1. 显示文章的作者,更新时间,等信息
  2. 其中文章所在栏目信息 $cate ,通过函数 fn_category,参数文章的cateId( 栏目Id)获取;

例子3:

<% str2html .article.Content %>

代码说明:

  1. 显示文章的正文;

例子4:

 <% $tags:=fn_tag_list_by_article .article.Tag %>
    <% if $tags %>
    <div class="grey-rule"></div>
      <div class="tags">标签:
         <% range $tags %>
            <a href="<%.Link%>" target="_blank"><% .TagName %></a>
         <% end %>
   </div>
<% end %>

代码说明:

  1. 显示文章的标签信息;
  2. 通过 fn_tag_list_by_article 函数,传递文章的标签参数 .article.Tag 获取;
  3. 遍历显示;

文章详情结构:

type ArticleDetail struct {
	Id          int64        //文章id
	CateId      int          //分类id
	ParentId    int          //父分类id
	RootId      int          //根分类id
	CateName    string       //最终分类名
	Uid         int64        //发布人id
	Username    string       //发布人昵称
	Title       string       //标签
	Photo       string       //图片
	Content     string       //内容
	Author      string       //作者
	Tag         string       //标签1,2,3
	Month       int          //月分
	State       int          //状态
	IsHot       int          //是否热门
	SeoTitle    string       //seo
	Keywords    string       //seo
	Description string       //seo
	IsComment   int          //是否可评论
	IP          string       //IP
	Updated     int          //更新时间
	Created     int          //入库时间
	SimpleTag   []*SimpleTag //展示用的标签列表
	MonthLink   string       //月份的地址
}

三、单页

自带变量:

  • page

例子1:

<title><%if .page.Title%><%.page.Title%><%else%><%.page.Name%><%end%></title>
<meta name="keywords" content="<% .page.Keywords %>" />
<meta name="description" content="<% .page.Description %>" />

代码说明:

  1. 如果设置有此单页的 Title 信息,则显示 Title,否则显示单页名称
  2. 显示栏目的Keywords
  3. 显示栏目的Description

例子2:

<div class="entry-content post-content" style="padding:4px 16px;">
    <%str2html .page.Content %>
</div>

代码说明:

  1. 显示单页的内容,使用str2html 转换为html格式;

单页的结构:

type SinglePage struct {
	PageId      int    //id
	Name        string //名称
	Remark      string //备注
	Content     string //页面内容 > html
	RouteUrl    string //路由地址
	Title       string //seo
	Keywords    string //seo
	Description string //seo
	State       int    //状态
	Views       int    //点击量
	Updated     int    //更新时间
	Created     int    //创建时间
}

三、标签页

自带变量:

  • tag

例子1:

<title><% if .tag.Title%><% .tag.Title%><%else%><% .tag.TagName%><%end%></title>
<meta name="keywords" content="<% .tag.Keywords %>" />
<meta name="description" content="<% .tag.Description %>" />

代码说明:

  1. 如果设置有此tag页的 Title 信息,则显示 Title,否则显示名称
  2. 显示栏目的Keywords
  3. 显示栏目的Description

例子2:

<% $tag:=fn_tag .tag.TagId %>
<a href="<% $tag.Link %>"><% $tag.TagName%></a>

代码说明:

  1. 使用 fn_tag 函数获取 tag的详细信息,赋值给 $tag;
  2. 显示 tag的链接和中文名称

例子3:

    <% $pageList:=fn_article_page_by_tag .page.Page 30 $tag.TagId %>
    <%range $pageList.Data%>
    <article class="col-sm-4">
        <div class="card card-full hover-a mb-module">
            <div class="ratio_360-202 image-wrapper">
                <a href="<%.Link%>">
                    <%if .Photo%>
                    <img src="<%.Photo%>" class="img-fluid article-photo" alt="<%.Title%>">
                    <% else %>
                    <img  src="/static/front/site/img/photo.png" class="img-fluid  article-photo" alt="<%.Title%>" >
                    <%end%>
                </a>
            </div>
            <div class="card-body">
                <h3 class="card-title h2 h3-sm h4-md h3-lg">
                    <a href="<% .Link%>"><% .Title%></a>
                </h3>
                <div class="card-text mb-2 text-muted small">
                    <time class="news-date" datetime="<% fn_datetime_format .Created%>"><% fn_datetime_format .Updated%></time>
                </div>
                <p class="card-text"></p>
            </div>
            <div class="gap-05"></div>
        </div>
    </article>
    <% end %>
    
    <div>
    <% fn_pager .page.Page 30 $pageList.Count $tag.Link | str2html %>
    </div>

代码说明:

  1. 使用 fn_article_page_by_tag 函数获取和当前标签相关的所有文章列表,赋值给 $pageList
  2. 遍历 $pageList中的文章信息
  3. 使用 fn_pager 显示标签页的文章翻页列表

标签的结构:

type Tag struct {
	TagId       int64  //标签id
	TagName     string //名称
	TagMatch    string //匹配
	Title       string //seo
	Keywords    string //seo
	Description string //seo
	State       int    //状态
	Updated     int    //更新时间
	Created     int    //创建时间
	Link        string //链接地址
}

标签对应文章列表的结构:

// TagList 标签列表
type TagList struct {
	ID          int64  //自动
	TagID       int64  //标签ID
	OID         int64  //文章ID
	CateId      int    //文章对应的分类ID
	Title       string //标题
	Description string //文章简介
	Photo       string //图片
	Author      string //作者
	State       int    //状态
	TagIds      string //多个标签的回写值
	Created     int    //文章的生成时间
	Link        string //文章的链接
}