GHOST CMS - Post 文章
Post
Use: {{#is "post"}}{{/is}}
to detect this context
使用:{{#is "post"}}{{/is}}检测此上下文
Description
Whenever you're viewing a single site post, you're in the post
context. The post
context is not set on static pages, which uses the page context instead.
当你浏览一个站点的帖子时,你是在帖子的上下文中。postcontext不是在静态页面上设置的,而是使用页面上下文。
Routes
The URL used to render a single post is configurable in the Ghost admin. The default is /:slug/
. Ghost also has an option for date-based permalinks, and can support many other formats if the setting is edited in the database.
用于呈现单个post的URL在Ghost admin中是可配置的。默认是/:slug/。Ghost还有一个基于日期的permalinks选项,如果在数据库中编辑了该设置,则可以支持许多其他格式。
Templates
The default template for a post is post.hbs
, this template is required in all Ghost themes.
Additionally, you can provide a custom template for a specific post. If there is a post-:slug.hbs
file with the :slug
matching the post's slug this will be used instead.
For example, if you have a '1.0 Announcement' post with the url /1-0-announcement/, adding a template called post-1-0-announcement.hbs
will cause that template to be used for the announcement post, instead of post.hbs
.
Another option is to use a "global" custom post template. If you add a template to your theme called custom-gallery.hbs
it will be available in a dropdown in the post settings menu so that it can be used on as many posts (or pages) as you want.
These templates exist in a hierarchy. Ghost looks for a template which matches the slug (post-:slug.hbs
) first, then looks for a custom template (custom-gallery.hbs
if selected in the post settings) and finally uses post.hbs
if no slug-specific template exists and no custom template is specified.
post的默认模板是post。哈佛商学院,这个模板是所有鬼主题所需要的。
此外,您可以为特定的帖子提供自定义模板。如果有一个后:蛞蝓。hbs文件与:蛞蝓匹配的职位的蛞蝓,这将被用来代替。
例如,如果您有一个带有url /1-0-announcement/的“1.0 Announcement”帖子,添加一个名为post-1-0-announcement的模板。hbs将使该模板用于公告发布,而不是post.hbs。
另一个选项是使用“全局”自定义post模板。如果您将模板添加到名为custom-gallery的主题中。它将在post settings菜单下拉菜单中提供,这样你就可以在任意多的文章(或页面)中使用它。
这些模板存在于层次结构中。Ghost首先查找匹配slug的模板(post-:slug.hbs),然后查找自定义模板(custom-gallery)。如果在post设置中选择hbs),最后使用post。如果不存在特定于弹头的模板,也不指定自定义模板,则为hbs。
Data
The post
context provides access to the post object which matches the route. As with all contexts, all of the @blog
global data is also available.
When outputting the post, you can use a block expression ({{#post}}{{/post}}
) to drop into the post scope and access all of the attributes. See a full list of attributes below:
post上下文提供与路由匹配的post对象的访问。与所有上下文一样,所有的@blog全局数据也可用。
在发布post时,您可以使用一个块表达式({ { } { / post })进入post范围并访问所有属性。参见下面的完整属性列表:
Post object attributes
- id - the Object ID of the post post的对象ID
- comment_id - The old, pre-1.0 incremental id of a post if present, or else the new Object ID 旧的,1.0之前的增加的id,如果存在,或者新的对象id
- title - the title of your site post 你网站文章的标题
- slug - slugified version of the title (used in urls and also useful for class names) 标题的slugified版本(用于url,对类名也很有用)
- excerpt - a short preview of your post content 一个简短的预览您的文章内容
- content - the content of the post 文章的内容
- url - the web address for the post page (see url helper) and special attributes. post页面的web地址(见url助手)和特殊属性
- feature_image - the cover image associated with the post 与文章相关的封面图片
- featured - indicates a featured post. Defaults to
表示一个特色文章。默认值为假false
- page -
true
if the post is a page. Defaults tofalse 如果文章是页面的话,为true,否则为false
- meta_title - custom meta title for the post 文章的meta title
- meta_description - custom meta description for the post
- published_at - date and time when the post was published
- updated_at - date and time when the post was last updated
- created_at - date and time when the post was created
- author - full details of the post's author
- tags - a list of tags associated with the post
- primary_tag - direct reference to the first tag associated with the post
Helpers
Using the {{#post}}{{/post}}
block expression is the key trick to having a happy time theming your post page. Once inside of the post, you can use any of these useful helpers (and many more) to output your post's data:
{{title}}
, {{content}}
, {{url}}
, {{author}}
, {{date}}
, {{excerpt}}
, {{img_url}}
, {{post_class}}
, {{tags}}
.
Example code
<!-- Everything inside the #post tags pulls data from the post -->
Special attributes
The post model is the most complex model in Ghost, and it has a couple of special attributes, which are calculated by the API rather than come direct from the DB.
URL
URL is a calculated, created based on the site's permalink setting and the post's other properties. It exists as a data attribute, but should always be output using the special {{url}}
helper rather than referenced as a data attribute.
That means always open a context and use {{url}}
explicitly, and is the same for allresources, but especially important because post has a data attribute present. So, always do {{#post}}{{url}}{{/post}}
. Never do {{post.url}}
.
Comment ID
Comment ID is a special attribute provided purely for compatibility reasons. In Ghost 1.0, there was a major change, which converted post IDs from being incremental IDs to being a longer 24 character ID. However, post IDs were used as identifiers in some cases, most notably when embedding Disqus comments.
For those sites, when the IDs changed after 1.0, Disqus was no longer able to associate comments with old posts correctly. Therefore we added {{comment_id}}
to solve that problem. We recommend always using {{comment_id}}
as your disqus identifier, or in any other scenario where IDs are used to identify content to 3rd parties.
Example: The Disqus config in Ghost should look like this, with the code inside a {{#post}}{{/post}}
block.
For a more complete example of Disqus integration see the Disqus code in Casper.
Primary tag
Each post has a list of 0 or more tags associated with it, which can be accessed via the tags
property and magical {{tags}}
helper. The first tag in the list is considered more important, and can be accessed via a special primary_tag
calculated property. This is a path expression, which points to a whole tag object, rather than a helper function.