movabletype.jp

アクション一覧を出力するテンプレートサンプル

登録した利用サービスのうち、アクションストリームに対応しているサービスのアクティビティを時系列に並べて出力できます。このドキュメントでは、アクションの一覧を出力する例として、最新10件のアクションを出力するウィジェットを作成してみます。

<mt:ActionStreams lastn="10">
    <mt:If name="__first__">
<div class="widget-recent-action widget-recent widget">
    <h3 class="widget-header">最近10件のアクション</a></h3>
    <div class="widget-content">
        <ul>
    </mt:If>
            <li class="service-<$mt:var name="service_type"$>"><$MTStreamAction$></li>
    <mt:If name="__last__">
</ul>
    </div>
</div>
    </mt:If>
</mt:ActionStreams>

サンプル01: 最近10件のアクションを出力

利用したテンプレートタグ

仕組み

  • 最新10件のアクションを出力したいので、mt:ActionStreams ブロックタグに lastn モディファイアを付与し、値を 10 とします。
  • mt:If ブロックタグに __first__ という変数の name モディファイアを付与しています。これは10件のアクションを出力するうち、先頭のものを出力するときにだけ、囲まれた内容を処理するように設定しています。
  • mt:Varservice_type という変数を参照し、ファンクションタグを使って、出力するサービス名を出力しています。
  • mt:StreamAction でアクションの詳細を出力しています。

特定のユーザーのアクションのみをリストする

[サンプル01] のテンプレートでは、テンプレートを使用するブログに属する全てのユーザーのアクションをリストします (特定の Author コンテキスト外での使用時)。これを特定ユーザーのアクションのみに限定したいときは、mt:ActionStreams ブロックタグに、アクションを出力したいユーザーの表示名を値に持つ display_name モディファイアを付与します。

<mt:ActionStreams lastn="10" display_name="Melody">
    <mt:If name="__first__">
<div class="widget-recent-action widget-recent widget">
    <h3 class="widget-header">最近10件のアクション</a></h3>
    <div class="widget-content">
        <ul>
    </mt:If>
            <li class="service-<$mt:var name="service_type"$>"><$MTStreamAction$></li>
    <mt:If name="__last__">
</ul>
    </div>
</div>
    </mt:If>
</mt:ActionStreams>

サンプル02: 出力するアクションを Melody のものに限定

複数のユーザーを指定したいときは、ユーザーの表示名をカンマ , で区切ります。

<mt:ActionStreams lastn="10" display_name="Melody,Nelson">
    <mt:If name="__first__">
<div class="widget-recent-action widget-recent widget">
    <h3 class="widget-header">最近10件のアクション</a></h3>
    <div class="widget-content">
        <ul>
    </mt:If>
            <li class="service-<$mt:var name="service_type"$>"><$MTStreamAction$></li>
    <mt:If name="__last__">
</ul>
    </div>
</div>
    </mt:If>
</mt:ActionStreams>

サンプル03: 出力するアクションを Melody と Nelson のものに限定

頻繁にアクションを出力するユーザーを変更したいときは、次のように変数を利用しても良いでしょう。

<$mt:SetVar name="display_names" value="Melody,Nelson"$>

    <mt:ActionStreams lastn="10" display_name="$display_names">
    <mt:If name="__first__">
<div class="widget-recent-action widget-recent widget">
    <h3 class="widget-header">最近10件のアクション</a></h3>
    <div class="widget-content">
        <ul>
    </mt:If>
            <li class="service-<$mt:var name="service_type"$>"><$MTStreamAction$></li>
    <mt:If name="__last__">
</ul>
    </div>
</div>
    </mt:If>
</mt:ActionStreams>

サンプル04: アクションを出力するユーザーを変数を使って設定