LoginSignup
11
8

More than 5 years have passed since last update.

Movable Type でメンテナンスしやすいテンプレート書き換え系のプラグインを書く

Last updated at Posted at 2016-02-12

TL; DR

以下のようにやると、なんとなく MTML を書けばだいたい思った通りに挿入され、またメンテナンスもしやすくてよいとおもいます。

説明で使うプラグインの概要

記事の編集画面のテンプレートを書き換える例で考えます。

ディレクトリ構造は以下の通りです。

AwesomePlugin
├── config.yaml
├── lib
│   └── MT
│       └── Plugin
│           └── AwesomePlugin.pm
└── tmpl
    ├── edit_entry_header.tmpl
    └── edit_entry_title.tmpl

ヘッダーやタイトルの付近に挿入するMTMLを、ファイルとして用意します。

  • edit_entry_header.tmpl : ヘッダーに挿入する MTML
  • <script type="text/javascript">
    jQuery(function($) {
    console.log("Awesome!");
    });
    </script>
    
  • edit_entry_title.tmpl : タイトル欄の後ろに挿入する MTML

  • <mt:If name="title">
    <div>
    <strong><__trans phrase="Awesome Title" /></strong>: <mt:Var name="title" encode_html="1" />
    </div>
    </mt:If>
    

template_param を使う

書き換えたいテンプレートを ${MT_HOME}/tmpl/cms から探して(今回であれば ${MT_HOME}/tmpl/cms/edit_entry.tmpl)、template_param.edit_entry のような形で指定します。

config.yaml は以下のようになります

id: AwesomePlugin
name: AwesomePlugin
version: 0.0.1

callbacks:
  MT::App::CMS::template_param.edit_entry: $AwesomePlugin::MT::Plugin::AwesomePlugin::template_param_edit_entry

description: <__trans phrase="This is my AwesomePlugin">

l10n_lexicon:
  ja:
    Awesome Title: "すごいタイトル"

テンプレートをロードして、挿入する

書き換えたいテンプレートの中から、id の指定されている MT タグを探して、そこにプラグインでロードしたテンプレートを挿入します。

AwesomePlugin.pm は以下のようになります。

package MT::Plugin::AwesomePlugin;

use strict;
use warnings;
use utf8;

sub plugin {
    MT->component( __PACKAGE__ =~ m/::([^:]+)$/ );
}

sub insert_after {
    my ( $tmpl, $id, $template_name ) = @_;

    my $before = $tmpl->getElementById($id);
    foreach my $t ( @{ plugin()->load_tmpl($template_name)->tokens } ) {
        $tmpl->insertAfter( $t, $before );
        $before = $t;
    }
}

sub insert_before {
    my ( $tmpl, $id, $template_name ) = @_;

    my $after = $tmpl->getElementById($id);
    foreach my $t ( @{ plugin()->load_tmpl($template_name)->tokens } ) {
        $tmpl->insertBefore( $t, $after );
    }
}

sub template_param_edit_entry {
    my ( $cb, $app, $param, $tmpl ) = @_;

    insert_before( $tmpl, 'header_include', 'edit_entry_header.tmpl' );
    insert_after( $tmpl, 'title', 'edit_entry_title.tmpl' );
}

1;

Perl のコードがよくわからない場合には、とりあえずは前半はどのプラグイでも使えるのでまるっとコピペして、以下の部分だけ書き換えれば挿入位置などを好きに変更することができます。

insert_before( $tmpl, 'header_include', 'edit_entry_header.tmpl' );
insert_after( $tmpl, 'title', 'edit_entry_title.tmpl' );

これで、edit_entry_header.tmpl と edit_entry_title.tmpl を記事の編集画面に挿入することができます。

この記事のやり方の特徴

template_param をそのまま使った場合の辛い点

プラグインから MT の管理画面のテンプレートを書き換える場合、

  • template_source
  • template_output
  • template_param

などがありますが (ドキュメント)、素で使おうとすると、

  • template_source や template_output
    • 正規表現の書き換えが辛くなりがち
    • コントローラーから渡されるパラメーターを利用できない
  • template_param
    • 置き換えるHTMLの構築が冗長になりがち

というのがなかなか辛いです。

この記事のやり方だと便利な点

  • 挿入する内容を MTML (もしくは単に HTML) として、別ファイルで管理できる
  • <__trans phrase="..." /> も普通に使える
    • template_source では使えない
11
8
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8