9月 20 2010

[MT]カスタムフィールドのテキストエリアに入力した改行をページに反映させる方法

カスタムフィールドのテキストエリアに入力した改行をページに反映させるには

 <$mt:CustomFieldHoge filters="__default__"$>

のように書く。

以上

【ネタ元】カスタムフィールドのテキストエリアに入力した改行をページに反映させる – 小粋空間
http://www.koikikukan.com/archives/2008/03/18-005038.php

7月 12 2010

[MT]今日以降の3ヶ月分のエントリー一覧をtable表示する

Posted by きつねのボタン in MovableType, MTテンプレ習作

前回、「[MT]今日以降の2ヶ月分のエントリー一覧を表示する」で、
* 今日以降のエントリーを
* 月ごとに表示する
のを試してみた。発展系でさらに、
* table表示に対応し、同月エントリー行の”月”欄はrowspanでくくられるようにする
* 月あたり最大10件まで表示
* 該当エントリーなしの月は月名だけの空行を表示する
ようにしてみた。

同月エントリー行の”月”をrowspanでくくるのが案外面倒だった。方針として、
* あらかじめその月の該当エントリーをリストアップして、該当エントリー数を取得 (rowspan=”N”に使う)
* 同時に、エントリーの内容もハッシュに保存しておく
* 最後に、一気に1ヶ月分のtableデータを出力
とすることで解決した。

こんなのを生成します

出力サンプル

完成形のテンプレート

<mt:Ignore><!--=== サブルーチンみたいなもん[MonthlyEvent] ===  --></mt:Ignore>
<mt:SetVarTemplate name="MonthlyEvent">

<mt:Ignore><!--  月ごとの変数を初期化  --></mt:Ignore>
<mt:SetVar name="monthly_cnt" value="0"><mt:Ignore><!--  今月のエントリー数カウンタ --></mt:Ignore>
<mt:SetVar name="tag_htmls" function="undef"><mt:Ignore><!--  ハッシュのクリア  --></mt:Ignore>

<mt:Ignore><!--  ハッシュにテーブル要素を蓄積  --></mt:Ignore>
<mt:Calendar month="$this_ym">
  <mt:CalendarIfToday>
    <mt:SetVar name="after_today" value="1">
    <mt:Ignore><!--  今日だったらフラグis_todayに"1"をセット  --></mt:Ignore>
  </mt:CalendarIfToday>

  <mt:If name="after_today" eq="1"><mt:Ignore><!--  今日以降なら  --></mt:Ignore>
    <mt:CalendarIfEntries>
    <mt:Entries>
      <mt:If name="monthly_cnt" lt="10"><mt:Ignore><!--  今月10件以下なら  --></mt:Ignore>
        <mt:Ignore><!--  ハッシュに値をセット  --></mt:Ignore>
        <mt:SetVarBlock name="tag_htmls" function="push">
          <td><mt:EntryDate format="%e日"><mt:EntryDate format="%a" setvar="youbi"><mt:if name="youbi" like="日"><span style="color:red">(日)</span><mt:elseif like="土"><span style="color:blue">(土)</span><mt:else>(<mt:Getvar name="youbi">)</mt:if></td>
          <td><a href="<mt:EntryPermalink abs2rel="1">" title="<mt:EntryTitle>"><mt:EntryTitle></a></td>
          </tr>
        </mt:SetVarBlock>
        <mt:SetVar name="monthly_cnt" op="++"><mt:Ignore><!--  今月のエントリー数カウンタを+1  --></mt:Ignore>
      </mt:If>
    </mt:Entries>
    </mt:CalendarIfEntries>
  </mt:If>
</mt:Calendar>

<mt:Ignore><!--  /ハッシュにテーブル要素を蓄積  --></mt:Ignore<mt:Ignore><!--  ここからテーブル要素出力  --></mt:Ignore>
<mt:If name="monthly_cnt" eq="0"><mt:Ignore><!--  0個の時  --></mt:Ignore>
  <tr>
  <td valign="top"><mt:Var name="this_month"></td>
  <td>(ないぉ)</td>
  <td>(ないぉ)</td>
  </tr>
<mt:Else>
  <mt:Loop name="tag_htmls" sort_by="key">
  <mt:If name="__first__"><mt:Ignore><!--  ≠0, 1つめheadder  --></mt:Ignore>
    <tr>
    <td valign="top" rowspan="<mt:Var name="monthly_cnt">"><mt:Var name="this_month"></td>
  <mt:Else><mt:Ignore><!--  ≠0, 2つめ以降headder  --></mt:Ignore>
    <tr>
  </mt:If>
  <MTVar name="__value__"><mt:Ignore><!--  ハッシュ値出力  --></mt:Ignore>
  </mt:Loop>
</mt:If><mt:Ignore><!--  /テーブル要素出力 ここまで  --></mt:Ignore>
</mt:SetVarTemplate>

<mt:Ignore><!--  === サブルーチンみたいなもん[GetNextMonth] ===  --></mt:Ignore>
<mt:SetVarTemplate name="GetNextMonth">
<mt:If name="this_month" eq="12"><!-- 12月なら年送り処理をする -->
  <mt:SetVar name="this_month" value="01">
  <mt:SetVar name="this_year" op="++">
<mt:Else>
  <mt:SetVar name="this_month" op="++">
</mt:If>
<mt:SetVarBlock name="this_ym"><mt:GetVar name="this_year"><mt:GetVar name="this_month" zero_pad="2"></mt:SetVarBlock>
</mt:SetVarTemplate>

<mt:Ignore><!-- 実際の処理はここから --></mt:Ignore>
<mt:SetVar name="after_today" value="0"><mt:Ignore><!-- 今日以降かの判定フラグを初期化 --></mt:Ignore>
<mt:Ignore><!--  再構築日(≒今日)の年をyyyymmフォーマットで取得  --></mt:Ignore>
<mt:Date format="%Y" setvar="this_year">
<mt:Date format="%b" setvar="this_month">
<mt:Date format="%Y%m" setvar="this_ym">

<table width="80%" border="1" cellpadding="2" cellspacing="2" id="event-info">
<tr>
<th>月</th>
<th>日</th>
<th><span>イベント内容</span></th>
</tr>

<mt:Ignore><!-- 今月の処理 --></mt:Ignore>
<mt:GetVar name="MonthlyEvent"

<mt:Ignore><!-- 来月の処理 --></mt:Ignore>
<mt:GetVar name="GetNextMonth">
<mt:GetVar name="MonthlyEvent">

<mt:Ignore><!-- さ来月の処理 --></mt:Ignore>
<mt:GetVar name="GetNextMonth">
<mt:GetVar name="MonthlyEvent">

</table>

解説:

処理本体部分:

実際の処理本体部分はこんな感じ。
繰り返し処理部分は2つのmt:SetVarTemplate でサブルーチン化しているので一見シンプル。
ちなみに、SetVarTemplateではそれぞれ↓こういう処理をしている(後述)。
* GetNextMonth: 翌月の年月を生成。翌月が年をまたぐ場合の処理。
MonthlyEvent: 該当月のエントリー数のカウント, 該当エントリーをハッシュに保存していく処理。

<mt:SetVar name="after_today" value="0"><mt:Ignore><!-- 今日以降かの判定フラグを初期化 --></mt:Ignore>
<mt:Ignore><!--  再構築日(≒今日)の年をyyyymmフォーマットで取得  --></mt:Ignore>
<mt:Date format="%Y" setvar="this_year">
<mt:Date format="%b" setvar="this_month">
<mt:Date format="%Y%m" setvar="this_ym">

ここで全体の初期値のセットを行っている。
after_today は”今日以降”のエントリーかを判定するフラグ。[MonthlyEvent]の中で、 mt:CalendarIfTodayがtrueになったら”1″にセットされて以降ずっと”1″のまま。
その後、↓tableの開始タグと、見だし行を出力。

<table width="80%" border="1" cellpadding="2" cellspacing="2" id="event-info">
<tr>
<th>月</th>
<th>日</th>
<th><span>イベント内容</span></th>
</tr>

これ以降、今月, 来月, さ来月のデータを出力(実処理はサブルーチン内で出力)

<mt:Ignore><!-- 今月の処理 --></mt:Ignore>
<mt:GetVar name="MonthlyEvent">

<mt:Ignore><!-- 来月の処理 --></mt:Ignore>
<mt:GetVar name="GetNextMonth">
<mt:GetVar name="MonthlyEvent">

<mt:Ignore><!-- さ来月の処理 --></mt:Ignore>
<mt:GetVar name="GetNextMonth">
<mt:GetVar name="MonthlyEvent">

最後に、tableの終了タグを出力しておしまい。

</table>

サブルーチン mt:SetVarTemplate name=”GetNextMonth”

翌月の年月を生成。翌月が年をまたぐ場合の処理をしている部分。

<mt:SetVarTemplate name="GetNextMonth">
<mt:If name="this_month" eq="12">
  <mt:SetVar name="this_month" value="01">
  <mt:SetVar name="this_year" op="++">
<mt:Else>
  <mt:SetVar name="this_month" op="++">
</mt:If>

前段で年をまたぐ場合の月送り処理をして、最後の行↓で、this_ym に”%Y%m”フォーマットのデータをセットしている。
mt:Calendar month=”$this_ym”で使えるフォーマット(YYYYMM)を得るためにわざわざ月と年にバラしてから月を+1して再び合体するということをしている。
月にzero_pad=”2″を指定している部分がキモ。ちと苦労した部分。

<mt:SetVarBlock name="this_ym">
  <mt:GetVar name="this_year"><mt:GetVar name="this_month" zero_pad="2">
</mt:SetVarBlock>
</mt:SetVarTemplate>

サブルーチン mt:SetVarTemplate name=”MonthlyEvent”

3つのパートに分かれている。最初はローカル変数の初期化みたいなことをしている部分。

<mt:Ignore><!--  月ごとの変数を初期化  --></mt:Ignore>
<mt:SetVar name="monthly_cnt" value="0"><mt:Ignore><!--  今月のエントリー数カウンタ --></mt:Ignore>
<mt:SetVar name="tag_htmls" function="undef"><mt:Ignore><!--  ハッシュのクリア  --></mt:Ignore>

2番目のパートではmt:Calendar month=”$this_ym” ブロックタグ内で、ここでは
* “今日以降”になったかどうかのフラグのセット。
* 今日以降なら指定月のエントリーをリストアップして、ハッシュに保存。
* 月のカウンター(monthly_cnt)により、(今日以降の)同月最大10件までのエントリーのみに限る部分の判定。
* このパートを回し終わると その月のエントリー数が得られる(monthly_cnt)。
を行っている。

<mt:Ignore><!--  ハッシュにテーブル要素を蓄積  --></mt:Ignore>
<mt:Calendar month="$this_ym">
<mt:CalendarIfToday>
<mt:SetVar name="after_today" value="1">
<mt:Ignore><!--  今日だったらフラグis_todayに"1"をセット  --></mt:Ignore></mt:CalendarIfToday>

<mt:If name="after_today" eq="1"><mt:Ignore><!--  今日以降なら  --></mt:Ignore>
<mt:CalendarIfEntries>
<mt:Entries>
<mt:If name="monthly_cnt" lt="10"><mt:Ignore><!--  今月10件以下なら  --></mt:Ignore>
<mt:Ignore><!--  ハッシュに値をセット  --></mt:Ignore>
<mt:SetVarBlock name="tag_htmls" function="push">
<td><mt:EntryDate format="%e日"><mt:EntryDate format="%a" setvar="youbi"><mt:if name="youbi" like="日"><span style="color:red">(日)</span><mt:elseif like="土"><span style="color:blue">(土)</span><mt:else>(<mt:Getvar name="youbi">)</mt:if></td>
<td><a href="<mt:EntryPermalink abs2rel="1">" title="<mt:EntryTitle>"><mt:EntryTitle></a></td>
</tr>
</mt:SetVarBlock>
<mt:SetVar name="monthly_cnt" op="++"><mt:Ignore><!--  今月のエントリー数カウンタを+1  --></mt:Ignore>
</mt:If>
</mt:Entries>
</mt:CalendarIfEntries>
</mt:If>
</mt:Calendar>
<mt:Ignore><!--  /ハッシュにテーブル要素を蓄積  --></mt:Ignore>

最後のパートで、1ヶ月分のtable要素を出力。
月のカラムをrowspanしたいために、2番目のパートであらかじめエントリー数(monthly_cnt)を数えておき、最後にまとめて出力処理する、ということになっている。

* <mt:Loop name=”tag_htmls” sort_by=”key”> ~</mt:Loop>ブロック内で1行ずつ順に出力。
* <mt:If name=”__first__”>で、その月の先頭行の場合は、<tr><td rowspan=”※エントリー数値をセット”>をセットしている。
* ハッシュの値を出す部分は、<MTVar name=”__value__”>。
* なお、その月のエントリーが一つもない時は(ないぉ)と表示。

<mt:Ignore><!--  ここからテーブル要素出力  --></mt:Ignore>
<mt:If name="monthly_cnt" eq="0"><mt:Ignore><!--  0個の時  --></mt:Ignore>
<tr>
<td valign="top"><mt:Var name="this_month"></td>
<td>(ないぉ)</td>
<td>(ないぉ)</td>
</tr>
<mt:Else>
<mt:Loop name="tag_htmls" sort_by="key">
<mt:If name="__first__"><mt:Ignore><!--  ≠0, 1つめheadder  --></mt:Ignore>
<tr>
<td valign="top" rowspan="<mt:Var name="monthly_cnt">"><mt:Var name="this_month"></td>
<mt:Else><mt:Ignore><!--  ≠0, 2つめ以降headder  --></mt:Ignore>
<tr>
</mt:If>
<MTVar name="__value__"><mt:Ignore><!--  ハッシュ値出力  --></mt:Ignore>
</mt:Loop>
</mt:If>
<mt:Ignore><!--  /テーブル要素出力 ここまで  --></mt:Ignore>

以上でっす。

7/12 追記

「今月10件以下なら・・・」の判定箇所が間違っていたので修正。mt:Entriesの外で判定すると、10件目近くで同日複数エントリーある場合に、10件を超えて表示されてしまう→ハッシュに保存する直前にmt:Ifを移動。

6月 29 2010

[MT]今日以降の2ヶ月分のエントリー一覧を表示する

Posted by きつねのボタン in MovableType, MTテンプレ習作

今日以降の2ヶ月分(今月と来月)のイベント情報を表示する、みたいなお題があって
ちょっとやってみた。なんかちょーダサダサなコードかもですがメモしておくー。

<ポイント>

  • 年送りの処理 : 今月が12月なら翌月は1にリセットし、かつ年を+1する。
  • <mt:Calendar month=”$this_ym”> で月をYYYYMM形式で渡してあげる必要があるので、$this_ymは年+月で算出し、
    かつ月はMM形式になるようzeropad=”2″を使って0を補完する必要あり。単に翌月=今月+1ってやると文字列でなく数値と解釈される模様。
<mt:SetVar name="after_today" value="0"><!--今日以降かの判定フラグを初期化-->

<!-- === サブルーチンみたいなもん === -->
<mt:SetVarTemplate name="MonthlyEvent">
<div>
<mt:Calendar month="$this_ym">
<mt:CalendarIfToday> <!-- 今日だったらフラグis_todayに"1"をセット -->
<mt:SetVar name="after_today" value="1">
</mt:CalendarIfToday>
<mt:If name="after_today" eq="1">  <!-- 今日以降なら -->
<mt:CalendarIfEntries>
<mt:Entries>
<p><$mt:CalendarDate format="%x"$> <$mt:EntryTitle$></p>
</mt:Entries>
</mt:CalendarIfEntries>
</mt:If>
</mt:Calendar>
</div>
</mt:SetVarTemplate>

<!-- 再構築日(≒今日)の年月をyyyymmフォーマットで取得 -->
<mt:Date format="%m" setvar="this_month">
<mt:Date format="%Y" setvar="this_year">
<mt:Date format="%Y%m" setvar="this_ym">

<h2>今月</h2>
<p>this_year:<mt:GetVar name="this_year"></p>
<p>this_month:<mt:GetVar name="this_month"></p>
<p>this_year_month: <mt:GetVar name="this_ym"></p>

<mt:GetVar name="MonthlyEvent">

<!--来月の処理-->
<mt:If name="this_month" eq="12">
<mt:SetVar name="this_month" value="01">
<mt:SetVar name="this_year" op="++">

<mt:Else>
<mt:SetVar name="this_month" op="++">
</mt:If>
<mt:SetVarBlock name="this_ym"><mt:GetVar name="this_year"><mt:GetVar name="this_month" zero_pad="2"></mt:SetVarBlock>

<h2>来月</h2>
<p>this_year:<mt:GetVar name="this_year"></p>
<p>this_month:<mt:GetVar name="this_month"></p>
<p>this_year_month: <mt:GetVar name="this_ym"></p>

<mt:GetVar name="MonthlyEvent">

↓出力サンプル
出力結果

5月 09 2010

[MT備忘録]サイトパス生成メモ

サイトパス生成メモ

<mt:BlogParentWebsite>
    <$mt:WebsiteURL setvar="website_url"$>
    <$mt:WebsiteHost setvar="website_host"$>
    <$mt:WebsiteRelativeURL setvar="website_path"$>
</mt:BlogParentWebsite>

ウェブサイトURL: http://example.com/foo/  とした場合、次のように出力される。

項目 使い方 出力例
ホストからの相対パス <$mt:Var name=’website_path’$>index.html /foo/index.html
URL <$mt:Var name=’website_url’$>index.html http://example.com/foo/index.html
SSL領域のURL https://<$mt:Var name=’website_host’$><$mt:Var name=’website_path’$>contact.html https://example.com/foo/contact.html
4月 08 2010

[MT備忘録]パンくず用メモ:ウェブページで自分がindex.htmlならパンくずのフォルダTOPを省略

Posted by きつねのボタン in MovableType, MTテンプレ習作

パンくずナビ用メモ。フォルダ1階層前提の猛烈シンプルなやつメモっておく。

  • ファイル名がindex.html の時 →  ホーム > ページタイトル(リンクなし)
  • ファイル名がindex以外の時  →  ホーム > フォルダ名(リンク) > ページタイトル
<li><a href="/">ホーム</a>&nbsp;&gt;</li>
<mt:unless tag="PageBasename" eq="index">
    <li><a href="./"><$mt:FolderLabel$></a>&nbsp;&gt;</li>
</mt:unless>
<li><$mt:PageTitle$></li>

もう少し使えそうなコード↓。

<mt:SetVarBlock name="topicpath">
    <a href="<$mt:Var name='website_path'$>index.html" title="ホーム">ホーム</a> &raquo;
    <mt:If name="main_index"><strong><$mt:BlogName$></strong>
    <mt:Else><a href="<$mt:Var name='blog_path'$>index.html" title="<$mt:BlogName$>"><$mt:BlogName$></a> &raquo;
    </mt:If>
</mt:SetVarBlock>

<mt:If name="page_archive">
    <mt:SetVarBlock name="topicpath" append="1">
        <mt:Unless tag="PageBasename" eq="index">
            <mt:ParentFolders>
                <a href="<$mt:BlogURL$><$mt:FolderPath$>"><$mt:FolderLabel$></a> &raquo;
            </mt:ParentFolders>
            <strong><$mt:PageTitle remove_html="1"$></strong>
        <mt:Else>
            <mt:ParentFolders exclude_current="1">
                <a href="<$mt:BlogURL$><$mt:FolderPath$>"><$mt:FolderLabel$></a> &raquo;
            </mt:ParentFolders>
            <strong><$mt:FolderLabel$></strong>
        </mt:Unless>
    </mt:SetVarBlock>
</mt:If>
2月 12 2010

[MT備忘録]「概要」が未入力時にmt:EntryExcerptで「本文」を挿入させない

Posted by in MovableType

「概要」が未入力ならmt:EntryExcerptには「本文」の指定文字数分が自動挿入されるのがデフォルトの動き。そうして欲しくない時があるんだけど、no_generate=”1″をつけてやれば回避できるらしい。

via. http://forums.movabletype.org/2009/05/testing-entryexcerpt-with-mtif.html (Dan Wolfgangセンセイのコメント)

<mt:EntryExcerpt no_generate="1" setvar="excerpt">
<mt:If name="excerpt">
 <mt:EntryExcerpt>
<mt:Else>
 <mt:EntryBody>
</mt:If>

movabletype.jpのタグリファレンスには書いてないし、 知らなかった。

movabletype.orgにはちゃんとあるのね (-.-)y-~~
http://www.movabletype.org/documentation/appendices/tags/entryexcerpt.html

1月 02 2010

MTTemplateNoteタグの長さはなんとかならんのかねーw

Posted by in MovableType

MTTemplateNoteというコメント用のタグがある。

<$mt:TemplateNote value="コメントhoge"$>

と書いて

<mt:Ignore>コメントhoge</mt:Ignore>

と同じように振る舞うんだけど
MTTemplateNoteってこのタグの長ったらしさはなんとかならないのかなー?!

上の2つを並べてみればわかるとおりMTTemplateNoteの方だとコメント本文の開始位置が恐ろしく後ろになるので、コメントとしての視認性が凄く悪い。全体の長さもMTIgnoreで囲む方がなんぼか短いし(エディタ上で確実に折り返されます。MTTemplateNoteさんw)

mt:Note にするとか、せめてvalue=”"の書式は勘弁してくれるとか。。。ねぇ? ^^;