article 类型

article 模板用于渲染文章页面,包含文章的完整内容,以及一个可选的评论区,供顾客留言。这种模板通常用于博客中的单篇文章。

小提示

可以参考 Dawn 中的 article 模板 以及它的 main section 来了解具体的实现方式。

Article 模板类型

所在位置

article 模板位于主题的 templates 目录中:

  1. └── theme
  2. ├── layout
  3. ├── templates
  4. | ├── 404.json
  5. | ├── article.json
  6. | ...
  7. ...

内容结构

你需要在 article 模板或其内部的 section 中包含以下内容:

article 对象

你可以通过 Liquid 的 article 对象 来展示文章的详细信息。

评论表单

你可以通过 Liquid 的 form 标签 以及 'new_comment', article 参数来添加评论表单。在 form 标签块内,你需要包含以下字段:

输入框 type name
Name text comment[author]
Email email comment[email]
Comment textarea comment[body]

示例代码如下:

  1. {% form 'new_comment', article %}
  2. {{ form.errors | default_errors }}
  3. <div class="name">
  4. <label for="name">Name</label>
  5. <input type="text" name="comment[author]" value="{{ form.author }}">
  6. </div>
  7. <div class="email">
  8. <label for="email">Email</label>
  9. <input type="email" name="comment[email]" value="{{ form.email }}">
  10. </div>
  11. <div class="comment">
  12. <label for="comment">Comment</label>
  13. <textarea name="comment[body]">{{ form.body }}</textarea>
  14. </div>
  15. <div class="submit">
  16. <input type="submit" value="Post">
  17. </div>
  18. {% endform %}

小提示

当顾客提交评论时,代码应该提供反馈,说明评论是否成功提交,或者是否出现了错误。

用法说明

使用 article 模板时,你还需要了解 分页显示文章评论 的方法。

小提示

如果你使用的是 JSON 模板,那么所有的 HTML 或 Liquid 代码都需要写在被模板引用的 section 中。

分页显示文章评论

文章的评论可以通过 article 对象 获取,每页最多显示 50 条评论。因此你应该使用 paginate 标签 来分页评论,确保所有评论都能被访问到:

  1. {% paginate article.comments by 20 %}
  2. {% for comment in article.comments %}
  3. <!-- 评论内容 -->
  4. {% endfor %}
  5. {{ paginate | default_pagination }}
  6. {% endpaginate %}