Ghost博客系统主题API

前言

Ghost博客系统的内容生成是采用标签来生成的和CMS有点相似。其实,从本质上来说每种博客系统都可以看做是一个CMS系统。由于我的博客之前是在wordpress,但是近来wordpress各种漏洞各种慢,已经让我无法忍受,所以,一怒之下就把博客移植到了Ghost,然而移植少不了的就是把原来的前端模板移植过来,所以今天就顺带说一下移植模板所要用到的Ghost的主题API。

用法

在Ghost博客系统里面全都是用了这样形式的标签,换言之我们如果要移植某个HTML模板到Ghost博客系统,只需要在我们所要写的内容标签里面填充好的Ghost 主题API相应的标签就可以了。

例如:
@blog.title }} ```
1
2
3
4
5
6
7
8

这样到时Ghost在相应的位置显示你博客的标题了,其他的标签也是这样如此类推。下面正式来介绍API。

# 正文

HELPER REFERENCE

**@blog**
{{@blog.url}}
1
博客网址
{{@blog.title}}
1
博客标题
{{@blog.description}}
1
博客描述
{{@blog.logo}}
1
博客logo地址
{{@blog.cover}}
1
博客封面
{{@blog.navigation}}
1
博客导航
{{@blog.permalinks}}
1
博客固定连接格式
{{@blog.posts_per_page}}
1
2
3
4
5
 每一个posts所含post的数量

**asset**

调用asset文件夹。用法:
{{ asset "css/style.css" }}
1
2
3


**author**
{{author.id}}
1
作者的id
{{author.name}}
1
作者的名字
{{author.slug}}
1
作者url的目录
{{author.bio}}
1
作者的bio
{{author.location}}
1
作者地址
{{author.website}}
1
作者网站
{{author.image}}
1
作者的头像
{{author.cover}}
1
作者的封面
{{author.url}}
1
2
3
4
5
6
7
8
9
10
 作者页url

**body_class**

{{body_class}} 为不同页面输出动态body的样式类。用法:

`<body class="{{body_class}}">`
[详情](http://themes.ghost.org/docs/structure#context-table)

**content**
{{content}}
1
2
3
4
5
 输出文章的内容。

**date**

输出日期时间。用法:
{{date format="YYYY MM DD HH mm ss"}}
1
2
3
4
5
 *Y代表年 M代表月 D代表日 H代表小时 m代表分钟 s代表秒*

**encode**

把标签的字符串解码为url可用的字符串。用法:
{{encode titile}}
1
2
3
4
5


**excerpt**

输出文章内容的片段。用法:
{{excerpt characters="140" words=“50”}}
1
2
3
4
5
 *characters代表字数 words代表单词数*

**foreach**

遍历一个标签数组里所有元素。用法:
{{#foreach posts}}code{{/foreach}}
1
2
3
4
5


**get**

获取标签数组。
{{#get posts}}
1
获取文章数组。
{{#get tags}}
1
获取标签数组。
{{#get users}}
1
2
3
4
5
6
7
 获取用户数组。

注:posts tags users 三者用法相似,它们都有各自元素的子项。

**ghost_foot**

输出博客系统页脚。用法:
{{ghost_foot}}
1
2
3
4
5


**ghost_head**

输出博客系统页头。用法:
{{ghost_head}}
1
2
3
4
5


**has**

条件判断,如果有就执行标签对内的代码。用法:
{{#has author="ecJon"}}code{{/has}}
1
2
3
4
5
6


**if**

条件判断,和has不一样if只针对某个标签是否有值。
用法:
{{#if image}}code{{else}}code{{/if}}
1
2
3
4
5
 //另外#if可以用^if表示取反。

**image**

输出image的路径。用法:
{{image}}
1
2
3
4
5


**is**

判断当前所处于的页面。用法:
{{#is "post, page"}}
1
2
3
4
5


**log**

输出debug信息。用法:
{{log post}}
1
2
3
4
5


**meta_description**

在meta里面输出描述信息。用法:
<meta name="description" content="{{meta_description}}" />
1
2
3
4

**meta_title**

输出meta的标题。用法:
<title>{{meta_title}}</title>
1
2
3
4

**navigation**

输出导航内容。例如输出导航栏。用法:
{{navigation}}
1
2
3
4
5


**page_url**

输出页面的url。用法:
{{page_url}}
1
2
3
4
5


**pagination**

输出翻页栏。用法:
{{pagination}}
1
2
3
4
5
6
7
8
9
10
11


其中,在翻页栏里可用的标签。
page - 当前页面的号码
prev - 上一页的号码
next - 下一页的号码
pages - 每个pages数组有效的元素数
total - 每个posts数组有效的元素数
limit - 每页的文章数

下面是这些标签的一段实例代码:
<nav class="pagination" role="navigation">
    {{#if prev}}
        <a class="newer-posts" href="{{page_url prev}}">&larr; Newer Posts</a>
    {{/if}}
    <span class="page-number">Page {{page}} of {{pages}}</span>
    {{#if next}}
        <a class="older-posts" href="{{page_url next}}">Older Posts &rarr;</a>
    {{/if}}
</nav>
1
2
3
4

**plural**

使标签数组在空、单数、复数的时候输出不同内容。用法:
{{plural value empty="" singular="" plural=""}}
1
2
3
4
5


**post**

文章标签对,在标签内可以使用post的所有子项。用法:
{{#post}}code{{/post}}
1
2
3
4
5
6
7


**post_class**

为不同文章输出不同的样式类,用法和body_class类似。

**prev_post & next_post**
{{prev_post}}
1
上一篇文章的url
{{next_post}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 下一篇文章的url

**tags**

储存文章标签的数组。

id - 标签的id
name - 标签名
description - 标签描述
image - 标签图像
meta_title - 标签的meta的标题
meta_description - 标签的meta的描述
url - 标签的页面的url

**title**

输出文章标题。用法:
{{title}}
1
2
3
4
5
6
7
8
9


**unless**

与if表相反意思。

**url**

输出url。用法:
{{url}}

`