Liquid Snippets by ALSEL
📄 ページテンプレート中級

ブログ記事とコメント欄の表示

ブログ記事の本文と作成日、著者を表示し、ブログがコメント機能を有効にしている場合は既存コメント一覧とコメント送信フォームをまとめて描画するテンプレート。コメント送信後の成功メッセージやバリデーションエラー表示に対応。

用途
Shopify の Online Store ブログ機能を使うとき、記事ページでコメント機能を実装したい場合に使う。ブログオーナーがコメント管理画面で承認・非承認を制御できる仕組みに対応。
設置場所
templates/article.liquid として配置する。ブログ記事ページにアクセスすると自動的にこのテンプレートが使用される。必要に応じて theme.liquid のレイアウト継承設定を確認する。
注意点
blog.comments_enabled? はブログ側の設定なので、Shopify 管理画面でブログのコメント機能が有効になっていることが前提。blog.moderated? で承認制コメントの有無を判定しているため、オーナーがコメント設定でモデレーション ON/OFF を決める必要がある。form.errors の判定は comment[author]、comment[email]、comment[body] の3フィールドに限定されており、新規フィールドを追加する場合は条件式も修正する。
タグ:blogarticlecommentformmoderation

コード

67 行 / liquid
<div class="article">
  <h3 class="article-head-title">{{ article.title }}</h3>
  <p> posted {{ article.created_at | date: "%Y %h" }} by {{ article.author }}</p>
  <div class="article-body textile">
    {{ article.content }}
  </div>
</div>

{% if blog.comments_enabled? %}
<div id="comments">
  <h3>Comments</h3>
  <!-- List all comments -->

  <ul>
  {% for comment in article.comments %}
    <li>
      <div class="comment">
        {{ comment.content }}
      </div>

      <div class="comment-details">
        Posted by {{ comment.author }} on {{ comment.created_at | date: "%B %d, %Y" }}
      </div>
    </li>
  {% endfor %}
  </ul>

  <!-- Comment Form -->
  {% form article %}
    <h3>Leave a comment</h3>

    <!-- Check if a comment has been submitted in the last request, and if yes display an appropriate message -->
    {% if form.posted_successfully? %}
      {% if blog.moderated? %}
        <div class="notice">
          Successfully posted your comment.<br />
          It will have to be approved by the blog owner first before showing up.
        </div>
      {% else %}
        <div class="notice">Successfully posted your comment.</div>
      {% endif %}
    {% endif %}

    {% if form.errors %}
      <div class="notice error">Not all the fields have been filled out correctly!</div>
    {% endif %}

    <dl>
      <dt class="{% if form.errors contains 'author' %}error{% endif %}"><label for="comment_author">Your name</label></dt>
      <dd><input type="text" id="comment_author" name="comment[author]" size="40" value="{{form.author}}" class="{% if form.errors contains 'author' %}input-error{% endif %}" /></dd>

      <dt class="{% if form.errors contains 'email' %}error{% endif %}"><label for="comment_email">Your email</label></dt>
      <dd><input type="text" id="comment_email" name="comment[email]" size="40" value="{{form.email}}" class="{% if form.errors contains 'email' %}input-error{% endif %}" /></dd>

      <dt class="{% if form.errors contains 'body' %}error{% endif %}"><label for="comment_body">Your comment</label></dt>
      <dd><textarea id="comment_body" name="comment[body]" cols="40" rows="5" class="{% if form.errors contains 'body' %}input-error{% endif %}">{{form.body}}</textarea></dd>
    </dl>

    {% if blog.moderated? %}
      <p class="hint">comments have to be approved before showing up</p>
    {% endif %}

    <input type="submit" value="Post comment" />
  {% endform %}
</div>
{% endif %}

出典・ライセンス

License:
MIT

このコードは Shopify 著作の MIT ライセンスソースです。 原本の著作権は Shopify が保有します。日本語訳は ALSEL によるものです。

関連項目

📄 ページテンプレート初級

ページテンプレート

Shopify の汎用ページテンプレート。ページのタイトルと本文コンテンツを Shopify 管理画面から取得して表示する基本的な構造。

📁 liquid·MIT·5
📄 ページテンプレート初級

リダイレクト

ヘッドレステーマで Shopify ストアフロントへのリダイレクト機能を提供するセクション。管理画面に「リダイレクト」という名称で登場し、ページから別 URL への誘導を実装する基盤となる。

📁 shopify-headless-theme·MIT·7
📄 ページテンプレート初級

トップページの基本テンプレート

Shopify Liquid の最小限のテンプレート例。HTML に日付変数を埋め込み、商品ページへのリンクを配置する基本構造を示す。

📁 liquid·MIT·7
📄 ページテンプレート初級

固定ページのテンプレート

Shopify 管理画面で作成した固定ページ(利用規約、プライバシーポリシーなど)を表示するテンプレート。ページタイトルと本文コンテンツを描画する。

📁 mcliquid-theme·MIT·8
📄 ページテンプレート初級

ページの基本テンプレート

固定ページ(About など)の基本レイアウト。ページタイトルとコンテンツを HTML の article 要素でマークアップし、管理画面で編集したテキストを表示する。

📁 liquid·MIT·9
📄 ページテンプレート初級

固定ページのテンプレート

固定ページ(ポリシー、会社概要、利用規約など)の基本レイアウト。ページタイトルと本文を中央揃えで表示する。

📁 ks-bootshop·MIT·10