article 类型
article
模板用于渲染文章页面,包含文章的完整内容,以及一个可选的评论区,供顾客留言。这种模板通常用于博客中的单篇文章。
小提示
可以参考 Dawn 中的 article 模板 以及它的 main section 来了解具体的实现方式。
所在位置
article
模板位于主题的 templates
目录中:
└── theme
├── layout
├── templates
| ├── 404.json
| ├── article.json
| ...
...
内容结构
你需要在 article
模板或其内部的 section 中包含以下内容:
article 对象
你可以通过 Liquid 的 article
对象 来展示文章的详细信息。
评论表单
你可以通过 Liquid 的 form
标签 以及 'new_comment', article
参数来添加评论表单。在 form
标签块内,你需要包含以下字段:
输入框 | type |
name |
---|---|---|
Name | text |
comment[author] |
email |
comment[email] |
|
Comment | textarea |
comment[body] |
示例代码如下:
{% form 'new_comment', article %}
{{ form.errors | default_errors }}
<div class="name">
<label for="name">Name</label>
<input type="text" name="comment[author]" value="{{ form.author }}">
</div>
<div class="email">
<label for="email">Email</label>
<input type="email" name="comment[email]" value="{{ form.email }}">
</div>
<div class="comment">
<label for="comment">Comment</label>
<textarea name="comment[body]">{{ form.body }}</textarea>
</div>
<div class="submit">
<input type="submit" value="Post">
</div>
{% endform %}
小提示
当顾客提交评论时,代码应该提供反馈,说明评论是否成功提交,或者是否出现了错误。
用法说明
使用 article
模板时,你还需要了解 分页显示文章评论 的方法。
小提示
如果你使用的是 JSON 模板,那么所有的 HTML 或 Liquid 代码都需要写在被模板引用的 section 中。
分页显示文章评论
文章的评论可以通过 article 对象 获取,每页最多显示 50 条评论。因此你应该使用 paginate 标签 来分页评论,确保所有评论都能被访问到:
{% paginate article.comments by 20 %}
{% for comment in article.comments %}
<!-- 评论内容 -->
{% endfor %}
{{ paginate | default_pagination }}
{% endpaginate %}