アルファサード有限会社ではPower CMS for MTをはじめとした自社製品の開発・保守を担当いただくPerlプログラマ(正社員)を募集しています(求人ページへ(Find Job!))。

2010年02月02日
ハッシュを活用してブログ記事とウェブページ混在のリストを表示する

ブログ記事/ウェブページ混在のエントリーのリスティングを行うテンプレートの実装例をご紹介します(Power CMS for MTのインストールされていない環境でも動作します)。

これを実現するにはMTテンプレートの「ハッシュ」を活用します。keyに日付をセットし(重複する可能性があるので、entry_idを末尾につけています)、 mt:entriesmt:pages のループの中でフィードの中身をvalueとしてハッシュにセットし、keyでソートして mt:loop で取り出します。

下記の例ではatom.xmlテンプレートにブログ記事+ウェブページを混在して表示させます。

<$mt:HTTPContentType type="application/atom+xml"$><?xml version="1.0" encoding="<$mt:PublishCharset$>"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title><$mt:BlogName remove_html="1" encode_xml="1"$></title>
    <link rel="alternate" type="text/html" href="<$mt:BlogURL encode_xml="1"$>" />
    <link rel="self" type="application/atom+xml" href="<$mt:Link template="feed_recent"$>" />
    <id>tag:<$mt:BlogHost exclude_port="1" encode_xml="1"$>,<$mt:TemplateCreatedOn format="%Y-%m-%d"$>:<$mt:BlogRelativeURL encode_xml="1"$>/<$mt:BlogID$></id>
    <updated><mt:Entries lastn="1"><$mt:EntryModifiedDate utc="1" format="%Y-%m-%dT%H:%M:%SZ"$></mt:Entries></updated>
    <mt:If tag="BlogDescription"><subtitle><$mt:BlogDescription remove_html="1" encode_xml="1"$></subtitle></mt:If>
    <generator uri="http://www.sixapart.com/movabletype/"><$mt:ProductName version="1"$></generator>

<$mt:SetVar name="lastn" value="15"$>
<$mt:SetVar name="entries" function="undef"$>

<mt:Entries lastn="$lastn">
<mt:SetVarBlock name="key"><mt:EntryDate format_name="iso8601">-<mt:EntryID></mt:SetVarBlock>
<mt:SetVarBlock name="entries" key="$key">
<entry>
    <title><$mt:EntryTitle remove_html="1" encode_xml="1"$></title>
    <link rel="alternate" type="text/html" href="<$mt:EntryPermalink encode_xml="1"$>" />
    <id><$mt:EntryAtomID$></id>

    <published><$mt:EntryDate utc="1" format="%Y-%m-%dT%H:%M:%SZ"$></published>
    <updated><$mt:EntryModifiedDate utc="1" format="%Y-%m-%dT%H:%M:%SZ"$></updated>

    <summary><$mt:EntryExcerpt remove_html="1" encode_xml="1"$></summary>
    <author>
        <name><$mt:EntryAuthorDisplayName encode_xml="1"$></name>
        <mt:If tag="EntryAuthorURL"><uri><$mt:EntryAuthorURL encode_xml="1"$></uri></mt:If>
    </author>
    <mt:EntryCategories>
        <category term="<$mt:CategoryLabel encode_xml="1"$>" scheme="http://www.sixapart.com/ns/types#category" />
    </mt:EntryCategories>
    <mt:EntryIfTagged><mt:EntryTags><category term="<$mt:TagName normalize="1" encode_xml="1"$>" label="<$mt:TagName encode_xml="1"$>" scheme="http://www.sixapart.com/ns/types#tag" />
    </mt:EntryTags></mt:EntryIfTagged>
    <content type="html" xml:lang="<$mt:BlogLanguage ietf="1"$>" xml:base="<$mt:BlogURL encode_xml="1"$>">
        <$mt:EntryBody encode_xml="1"$>
        <$mt:EntryMore encode_xml="1"$>
    </content>
</entry>
</mt:SetVarBlock>
</mt:Entries>

<mt:Pages lastn="$lastn">
<mt:SetVarBlock name="key"><mt:PageDate format_name="iso8601">-<mt:PageID></mt:SetVarBlock>
<mt:SetVarBlock name="entries" key="$key">
<entry>
    <title><$mt:PageTitle remove_html="1" encode_xml="1"$></title>
    <link rel="alternate" type="text/html" href="<$mt:PagePermalink encode_xml="1"$>" />
    <id><$mt:EntryAtomID$></id>

    <published><$mt:PageDate utc="1" format="%Y-%m-%dT%H:%M:%SZ"$></published>
    <updated><$mt:PageModifiedDate utc="1" format="%Y-%m-%dT%H:%M:%SZ"$></updated>

    <summary><$mt:PageExcerpt remove_html="1" encode_xml="1"$></summary>
    <author>
        <name><$mt:PageAuthorDisplayName encode_xml="1"$></name>
        <mt:If tag="PageAuthorURL"><uri><$mt:PageAuthorURL encode_xml="1"$></uri></mt:If>
    </author>
    <mt:PageFolder>
        <category term="<$mt:FolderLabel encode_xml="1"$>" scheme="http://www.sixapart.com/ns/types#category" />
    </mt:PageFolder>
    <mt:PageIfTagged><mt:PageTags><category term="<$mt:TagName normalize="1" encode_xml="1"$>" label="<$mt:TagName encode_xml="1"$>" scheme="http://www.sixapart.com/ns/types#tag" />
    </mt:PageTags></mt:PageIfTagged>
    <content type="html" xml:lang="<$mt:BlogLanguage ietf="1"$>" xml:base="<$mt:BlogURL encode_xml="1"$>">
        <$mt:PageBody encode_xml="1"$>
        <$mt:PageMore encode_xml="1"$>
    </content>
</entry>
</mt:SetVarBlock>
</mt:Pages>

<$mt:SetVar name="i" value="0"$>
<mt:Loop name="entries" sort_by="key reverse">
<$mt:SetVar name="i" op="++"$>
<mt:If name="i" le="$lastn">
<$mt:GetVar name="__value__"$>
</mt:If>
</mt:Loop>

</feed>

Power CMS for MTのサポートへお問い合わせいただいた際にプラグインを書くと言う方法を検討したのですが、標準のテンプレートタグで出来るという話がスタッフからあがり、このテンプレートを作成しました。かなりプログラミングライクなことがMTMLで出来ることがおわかりいただけると思います。

カテゴリー:テンプレート作成Tips

トラックバック

このエントリーのTBPingURL:
http://powercms.alfasado.net/mte/mt-tb-powercms.cgi/63

コメントを投稿する

TypeKey IDを使ってサインインしてください。

(初めてコメント投稿される方は、承認が必要になることがあります。承認されるまではコメントは表示されませんのでご了承ください)

  • Alfasado
Alfasado