mtde.info

Movable Type Developers Express

このカテゴリでは、Feedの取り扱いに関するサンプルを紹介します。

『Feed関連サンプル』カテゴリ内のエントリ一覧

素ノーマルAtomテンプレート

2008年2月17日 23:31 | Writer: apstar | 記事本文

Movable Type 4での、デフォルトのatom.xmlテンプレートコードです。

管理ページで、「デザイン」→「テンプレート」と進み、インデックステンプレートページにて「Atom」をクリックしてatom.xml生成のテンプレートを開きます。

<$MTHTTPContentType type="application/atom+xml"$><?xml version="1.0" encoding="<$MTPublishCharset$>"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title><$MTBlogName remove_html="1" encode_xml="1"$></title>
    <link rel="alternate" type="text/html" href="<$MTBlogURL encode_xml="1"$>" />
    <link rel="self" type="application/atom+xml" href="<$MTLink template="atom"$>" />
    <id>tag:<$MTBlogHost exclude_port="1" encode_xml="1"$>,<$MTTemplateCreatedOn format="%Y-%m-%d"$>:<$MTBlogRelativeURL encode_xml="1"$>/<$MTBlogID$></id>
    <updated><MTEntries lastn="1"><$MTEntryModifiedDate utc="1" format="%Y-%m-%dT%H:%M:%SZ"$></MTEntries></updated>
    <MTIfNonEmpty tag="MTBlogDescription"><subtitle><$MTBlogDescription remove_html="1" encode_xml="1"$></subtitle></MTIfNonEmpty>
    <generator uri="http://www.sixapart.com/movabletype/"><$MTProductName version="1"$></generator>
<MTEntries lastn="15">
<entry>
    <title><$MTEntryTitle remove_html="1" encode_xml="1"$></title>
    <link rel="alternate" type="text/html" href="<$MTEntryPermalink encode_xml="1"$>" />
    <id><$MTEntryAtomID$></id>

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

    <summary><$MTEntryExcerpt remove_html="1" encode_xml="1"$></summary>
    <author>
        <name><$MTEntryAuthorDisplayName encode_xml="1"$></name>
        <MTIfNonEmpty tag="MTEntryAuthorURL"><uri><$MTEntryAuthorURL encode_xml="1"$></uri></MTIfNonEmpty>
    </author>
    <MTEntryCategories>
        <category term="<$MTCategoryLabel encode_xml="1"$>" scheme="http://www.sixapart.com/ns/types#category" />
    </MTEntryCategories>
    <MTEntryIfTagged><MTEntryTags><category term="<$MTTagName normalize="1" encode_xml="1"$>" label="<$MTTagName encode_xml="1"$>" scheme="http://www.sixapart.com/ns/types#tag" />
    </MTEntryTags></MTEntryIfTagged>
    <content type="html" xml:lang="<$MTBlogLanguage ietf="1"$>" xml:base="<$MTBlogURL encode_xml="1"$>">
        <$MTEntryBody encode_xml="1"$>
        <$MTEntryMore encode_xml="1"$>
    </content>
</entry>
</MTEntries>
</feed>

素ノーマルRSSテンプレート

2008年2月17日 23:37 | Writer: apstar | 記事本文

Movable Type 4での、デフォルトのrss.xmlテンプレートコードです。

管理ページで、「デザイン」→「テンプレート」と進み、インデックステンプレートページにて「RSS」をクリックしてrss.xml生成のテンプレートを開きます。

<$MTHTTPContentType type="application/rss+xml"$><?xml version="1.0" encoding="<$MTPublishCharset$>"?>
<rss version="2.0">
    <channel>
        <title><$MTBlogName remove_html="1" encode_xml="1"$></title>
        <link><$MTBlogURL$></link>
        <description><$MTBlogDescription remove_html="1" encode_xml="1"$></description>
        <language><$MTBlogLanguage ietf="1"$></language>
        <copyright>Copyright <$MTDate format="%Y"$></copyright>
        <lastBuildDate><MTEntries lastn="1"><$MTEntryDate format_name="rfc822"$></MTEntries></lastBuildDate>
        <generator>http://www.sixapart.com/movabletype/</generator>
        <docs>http://www.rssboard.org/rss-specification</docs>
        <MTEntries lastn="15">
        <item>
            <title><$MTEntryTitle remove_html="1" encode_xml="1"$></title>
            <description><$MTEntryBody encode_xml="1"$></description>
            <link><$MTEntryPermalink encode_xml="1"$></link>
            <guid><$MTEntryPermalink encode_xml="1"$></guid>
            <MTEntryCategories>
                <category domain="http://www.sixapart.com/ns/types#category"><$MTCategoryLabel remove_html="1" encode_xml="1"$></category>
            </MTEntryCategories>
            <MTEntryIfTagged><MTEntryTags>
                <category domain="http://www.sixapart.com/ns/types#tag"><$MTTagName remove_html="1" encode_xml="1"$></category>
            </MTEntryTags></MTEntryIfTagged>
            <pubDate><$MTEntryDate format_name="rfc822"$></pubDate>
        </item>
        </MTEntries>
    </channel>
</rss>

特定カテゴリのみのatom.xml

2008年2月18日 00:26 | Writer: apstar | 記事本文

サイト全てではなく、特定のひとつのカテゴリの更新情報を、専用のatom.xmlで出力するサンプルです。

手順1:Atom(カテゴリ専用)テンプレートを作成する

まず、管理ページで、「デザイン」→「テンプレート」と進み、インデックステンプレートページにて「Atom」をクリックして、テンプレートコードをコピーします。

次に「インデックステンプレートを作成」をクリックし、カテゴリ専用のAtomテンプレートを作成します。名前は「Atom(xxxカテゴリ専用)」、出力ファイル名は「atom_xxx.xml」などとします。xxxは任意の文字列です。出力ファイル名には使用文字を半角英数のみとし、日本語や全角カナなどを使用しないでください。

以下コードは、デフォルトのAtomテンプレートを改造した、「Movable Type」というカテゴリ専用のAtomを、atom_mt4.xmlというファイル名で出力する場合のサンプルコードです。

変更箇所は、基本的には「MTEntriesで出力する際のカテゴリ」、「表示ディスクリプション」、「atom_mt4.xmlのリンク先」の3箇所です。

<$MTHTTPContentType type="application/atom+xml"$><?xml version="1.0" encoding="<$MTPublishCharset$>"?>

<MTSetVar name="TargetCat" value="Movable Type">
<MTSetVarBlock name="CatDescription">(<MTGetVar name="TargetCat">カテゴリ専用)</MTSetVarBlock>
<MTSetVarBlock name="CatXmlLink"><MTBlogURL>atom_mt4.xml</MTSetVarBlock>

<feed xmlns="http://www.w3.org/2005/Atom">
    <title><$MTBlogName remove_html="1" encode_xml="1"$><MTGetVar name="CatDescription" remove_html="1" encode_xml="1"></title>
    <link rel="alternate" type="text/html" href="<$MTBlogURL encode_xml="1"$>" />
    <link rel="self" type="application/atom+xml" href="<MTGetVar name="CatXmlLink" remove_html="1" encode_xml="1">" />
    <id>tag:<$MTBlogHost exclude_port="1" encode_xml="1"$>,<$MTTemplateCreatedOn format="%Y-%m-%d"$>:<$MTBlogRelativeURL encode_xml="1"$>/<$MTBlogID$></id>
    <updated><MTEntries lastn="1"><$MTEntryModifiedDate utc="1" format="%Y-%m-%dT%H:%M:%SZ"$></MTEntries></updated>
    <MTIfNonEmpty tag="MTBlogDescription"><subtitle><$MTBlogDescription remove_html="1" encode_xml="1"$></subtitle></MTIfNonEmpty>
    <generator uri="http://www.sixapart.com/movabletype/"><$MTProductName version="1"$></generator>
<MTEntries category="$TargetCat" lastn="15">
<entry>
    <title><$MTEntryTitle remove_html="1" encode_xml="1"$></title>
    <link rel="alternate" type="text/html" href="<$MTEntryPermalink encode_xml="1"$>" />
    <id><$MTEntryAtomID$></id>

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

    <summary><$MTEntryExcerpt remove_html="1" encode_xml="1"$></summary>
    <author>
        <name><$MTEntryAuthorDisplayName encode_xml="1"$></name>
        <MTIfNonEmpty tag="MTEntryAuthorURL"><uri><$MTEntryAuthorURL encode_xml="1"$></uri></MTIfNonEmpty>
    </author>
    <MTEntryCategories>
        <category term="<$MTCategoryLabel encode_xml="1"$>" scheme="http://www.sixapart.com/ns/types#category" />
    </MTEntryCategories>
    <MTEntryIfTagged><MTEntryTags><category term="<$MTTagName normalize="1" encode_xml="1"$>" label="<$MTTagName encode_xml="1"$>" scheme="http://www.sixapart.com/ns/types#tag" />
    </MTEntryTags></MTEntryIfTagged>
    <content type="html" xml:lang="<$MTBlogLanguage ietf="1"$>" xml:base="<$MTBlogURL encode_xml="1"$>">
        <$MTEntryBody encode_xml="1"$>
        <$MTEntryMore encode_xml="1"$>
    </content>
</entry>
</MTEntries>
</feed>

手順2:ヘッダーへ宣言文を作成する

「デザイン」→「テンプレート」と進み、テンプレートモジュールページにて「ヘッダー」をクリックして、以下のようなコードを書き足します。

以下コードはインデックスページであれば宣言が追加されるようにした例です。MTIfの部分を書き換えて、当該カテゴリページでのみ出力としてもいいでしょう。

<MTIf name="main_index">
   <link rel="alternate" type="application/atom+xml" title="Atom" href="<MTBlogURL>atom_mt4.xml" />
</MTIf>

特定カテゴリのみのrss.xml

2008年2月18日 00:42 | Writer: apstar | 記事本文

サイト全てではなく、特定のひとつのカテゴリの更新情報を、専用のrss.xmlで出力するサンプルです。

手順1:RSS(カテゴリ専用)テンプレートを作成する

まず、管理ページで、「デザイン」→「テンプレート」と進み、インデックステンプレートページにて「RSS」をクリックして、テンプレートコードをコピーします。

次に「インデックステンプレートを作成」をクリックし、カテゴリ専用のRSSテンプレートを作成します。名前は「RSS(xxxカテゴリ専用)」、出力ファイル名は「rss_xxx.xml」などとします。xxxは任意の文字列です。出力ファイル名には使用文字を半角英数のみとし、日本語や全角カナなどを使用しないでください。

以下コードは、デフォルトのRSSテンプレートを改造した、「Movable Type」というカテゴリ専用のRSSを、atom_mt4.xmlというファイル名で出力する場合のサンプルコードです。

変更箇所は、基本的には「MTEntriesで出力する際のカテゴリ」、「表示ディスクリプション」の2箇所です。

<$MTHTTPContentType type="application/rss+xml"$><?xml version="1.0" encoding="<$MTPublishCharset$>"?>
<rss version="2.0">

    <MTSetVar name="TargetCat" value="Movable Type">
    <MTSetVarBlock name="CatDescription">(<MTGetVar name="TargetCat">カテゴリ専用)</MTSetVarBlock>
    
    <channel>
        <title><$MTBlogName remove_html="1" encode_xml="1"$><MTGetVar name="CatDescription" remove_html="1" encode_xml="1"></title>
        <link><$MTBlogURL$></link>
        <description><$MTBlogDescription remove_html="1" encode_xml="1"$><MTGetVar name="CatDescription" remove_html="1" encode_xml="1"></description>
        <language><$MTBlogLanguage ietf="1"$></language>
        <copyright>Copyright <$MTDate format="%Y"$></copyright>
        <lastBuildDate><MTEntries category="$TargetCat" lastn="1"><$MTEntryDate format_name="rfc822"$></MTEntries></lastBuildDate>
        <generator>http://www.sixapart.com/movabletype/</generator>
        <docs>http://www.rssboard.org/rss-specification</docs>
        <MTEntries category="$TargetCat" lastn="15">
        <item>
            <title><$MTEntryTitle remove_html="1" encode_xml="1"$></title>
            <description><$MTEntryBody encode_xml="1"$></description>
            <link><$MTEntryPermalink encode_xml="1"$></link>
            <guid><$MTEntryPermalink encode_xml="1"$></guid>
            <MTEntryCategories>
                <category domain="http://www.sixapart.com/ns/types#category"><$MTCategoryLabel remove_html="1" encode_xml="1" cutfirstchar="3"$></category>
            </MTEntryCategories>
            <MTEntryIfTagged><MTEntryTags>
                <category domain="http://www.sixapart.com/ns/types#tag"><$MTTagName remove_html="1" encode_xml="1"$></category>
            </MTEntryTags></MTEntryIfTagged>
            <pubDate><$MTEntryDate format_name="rfc822"$></pubDate>
        </item>
        </MTEntries>
    </channel>
</rss>

手順2:ヘッダーへ宣言文を作成する

「デザイン」→「テンプレート」と進み、テンプレートモジュールページにて「ヘッダー」をクリックして、以下のようなコードを書き足します。

以下コードはインデックスページであれば宣言が追加されるようにした例です。MTIfの部分を書き換えて、当該カテゴリページでのみ出力としてもいいでしょう。

<MTIf name="main_index">
   <link rel="alternate" type="application/rss+xml" title="RSS" href="<MTBlogURL>rss_mt4.xml" />
</MTIf>

Feed関連テンプレートタグ

2008年2月18日 23:30 | Writer: apstar | 記事本文

以下は、Feed受信に関するさまざまな情報をテンプレートタグで取得するためのコードサンプルです。

Feed関連のテンプレートタグは、Atom・RSSどちらも、書き方や再構築時の挙動は同じです。以下の例では、サイトルートにあるRSS(rss.xml)について調査を行うテンプレートを用いています。

Feed情報を取得する

rss.xmlからFeed情報を取得するサンプルコードです。

<h3>rss.xmlを用いる場合のFeed情報</h3>
<MTSetVarBlock name="FeedUrl"><MTBlogURL>rss.xml</MTSetVarBlock>

<MTFeed uri="$FeedUrl">
<ul>
   <li>MTFeedbackScore=<MTFeedbackScore></li>
   <li>MTFeedInclude uri="$FeedUrl"=<MTFeedInclude uri="$FeedUrl"></li>
   <li>MTFeedLink=<MTFeedLink></li>
   <li>MTFeedTitle=<MTFeedTitle></li>
</ul>
</MTFeed>

<h3>rss.xmlを用いる場合のエントリのFeed情報</h3>
<MTFeed uri="$FeedUrl">
<ul>
<MTFeedEntries lastn="5">
<li>MTFeedEntryLink=<MTFeedEntryLink></li>
<li>MTFeedEntryTitle=<MTFeedEntryTitle></li>
</MTFeedEntries>
</ul>
</MTFeed>

rss.xmlとatom.xmlについては、結果は同じです。

MTFeed
指定したRSS Feedの内容を出力するためのブロックタグです。Feed関連のファンクションタグを実行するには、このMTFeedブロック内に置かねばいけません。urlモデファイアには、データを表示させるFeedのURLを入力します。
MTFeedbackScore
リファレンスには「迷惑コメント/トラックバックを判断するための評価の値を表示する」、とあります。現状では何も出力されません。
MTFeedInclude
指定したFeed のリストを簡単に表示するためのファンクションタグです。urlモデファイアには、データを表示させるFeedのURLを入力します(リファレンスには記述がありませんが、このファンクションタグにもuriモデファイアが必要です)。lastn="N"モデファイアで、表示させる記事の件数を指定できます。
MTFeedLink
指定したFeedを持つサイトへのURLを出力します。つまり「http://www.mtde.info/」などと出力します。
MTFeedTitle
読み込んだFeedを持つウェブサイトの名前を出力します。
MTFeedEntries
MTFeedブロックタグで読み込む設定を行ったフィードの内容を表示するためのブロックタグです。MTEntriesとよく似た挙動です。lastn,offsetモディファイアで出力件数を選択可能です。
MTFeedEntryLink
読み込んだFeedにあるエントリあるいはウェブページへのリンクを出力します。
MTFeedEntryTitle
読み込んだFeedにあるエントリあるいはウェブページのタイトルを出力します。

Feedを受信してリストを生成する(1)

2008年2月18日 23:49 | Writer: apstar | 記事本文

Feed情報を受信してリストを作成するサンプルです。

コード

サイトルートのrss.xmlより、最新記事を10件取得し、リスト化するオーソドックスなリストです。FeedのパスはFeedUrl変数へ格納され、MTFeedでuriモデファイア指定時に使用されます。

<MTSetVarBlock name="FeedUrl"><MTBlogURL>rss.xml</MTSetVarBlock>
<MTFeed uri="$FeedUrl">
   <h3><MTFeedTitle></h3>
   <ul>
   <MTFeedEntries lastn="10">
      <li><a href="<MTFeedEntryLink>"><MTFeedEntryTitle></a></li>
   </MTFeedEntries>
   </ul>
</MTFeed>

Feedを受信してリストを生成する(2)

2008年2月19日 00:09 | Writer: apstar | 記事本文

Feed情報を受信してリストを作成するサンプルです(その2)。

こちらではMTFeedIncludeファンクションタグを用い、より簡素なコードでリスト化を行います。

コード

サイトルートのrss.xmlより最新記事を3件、atom.xmlより最新記事を5件取得し、リスト化するオーソドックスなリストです。MTFeedIncludeファンクションタグには、uriモデファイアが必要です。

<p>RSSから3件取得</p>
<MTSetVarBlock name="FeedUrl"><MTBlogURL>rss.xml</MTSetVarBlock>
<MTFeed uri="$FeedUrl">
   <MTFeedInclude uri="$FeedUrl" lastn="3">
</MTFeed>

<p>Atomから5件取得</p>
<MTSetVarBlock name="FeedUrl"><MTBlogURL>atom.xml</MTSetVarBlock>
<MTFeed uri="$FeedUrl">
   <MTFeedInclude uri="$FeedUrl" lastn="5">
</MTFeed>

外部のサイトのFeedを受信してリストを生成する(1)

2008年2月19日 00:14 | Writer: apstar | 記事本文

外部のサイトのフィード情報を受信してリストを作成するサンプルです。

この手法では、ブログの再構築をしなくても、ページを観覧した際に、常に最新の情報を取得できます。

必要なもの

以下のファイルが必要です。

rssv.php、rssv2.phpの入手先や詳しい資料は以下サイトです。

RSS.php、Parser.php、PEAR.phpの公式サイトは以下です。

外部のサイトのFeedを受信してリストを生成する(1)の続きを読む

外部のサイトのFeedを受信してリストを生成する(2)

2008年2月19日 00:26 | Writer: apstar | 記事本文

外部のサイトのフィード情報を受信してリストを作成するサンプルです(その2)。さきの「外部のサイトのFeedを受信してリストを生成する(1)」サンプルより、圧倒的に簡単です。

ただし、この手法では、ブログの再構築時のFeed情報でリストを作成します。リストを常に最新の情報にするには、再構築の必要があります

1.MTFeedInclude を使用したサンプルコード(A)

小回りは効きませんが、簡単です。uriモデファイアを外部サイトのFeedリンクに変更するだけです。

<p>MTFeedIncludeを使用する</p>
<MTSetVar name="FeedUrl" value="http://www.sixapart.jp/index.xml">
<MTFeed uri="$FeedUrl">
   <MTFeedInclude uri="$FeedUrl" lastn="7">
</MTFeed>

2.MTFeedInclude を使用しないサンプルコード(B)

少々htmlタグを変更する場合は、こちらのほうが向いています。

<p>MTFeedIncludeを使用しない</p>
<MTSetVar name="FeedUrl" value="http://www.sixapart.jp/index.xml">
<MTFeed uri="$FeedUrl">
   <h3><MTFeedTitle></h3>
   <ul>
   <MTFeedEntries lastn="7">
      <li><a href="<MTFeedEntryLink>"><MTFeedEntryTitle></a></li>
   </MTFeedEntries>
   </ul>
</MTFeed>
エントリ
タグ
はじめに
導入編
ローカル環境
変数
サンプル
その他
トラブル集
2008 mtde.info