プラグイン開発・モディファイア拡張をconfig.yamlで書く
trim_to モディファイアを拡張してみます。
trim_to
http://www.movabletype.jp/documentation/appendices/modifiers/trim_to.html
最初の N 文字を取り出します。N はアトリビュートの値として設定します。
使い方
目標実行結果
エントリータイトル「モディファイアを拡張してみます」
通常、
<mt:entrytitle trim_to="10" /> #=> モディファイアを拡張
となるところを、
<mt:entrytitle trim_to="10","..." /> #=> モディファイアを拡張...
と続き文字を指定できるようにします。
まずはconfig.yaml
モディファイアのconfig.yamlの書き方がなかなか見つからなくて苦労しました><
config.yaml
id: TrimExpand
name: TrimExpand
description: 'Trim拡張プラグイン'
author_name: takeru-c
author_link: http://www.hachikun.com
version: 0.1
tags:
modifier:
trim_to:
handler: $TrimExpand::TrimExpand::_fltr_trim_to
変数タグや、ブロックタグなら、
tags:
function:
xxxxx: $Example::Example::xxxxx
block:
yyyyy: $Example::Example::yyyyyと書けるのに、モディファイアは一段落多く、「handler:」が必要みたいです。
参考:http://www.movabletype.org/documentation/developer/template-tag-modifiers.html
そしてサブルーチンの中身
TrimExpand.pm ※修正しました20090210
package TrimExpand; use strict; use MT::I18N qw( substr_text length_text ); sub _fltr_trim_to{ my ($str, $val, $ctx) = @_; my ($value,$trailing) = ( ref($val) eq 'ARRAY' ? ($val->[0],$val->[1]) : ($val,'') ); $str = substr_text($str, 0, $value).$trailing if $value < length_text($str); $str; } 1;
こうすることで、
どっちも使用することができます。
ちなみにディレクトリ構成
pligins
`-- TrimExpand
|-- config.yaml
`-- lib
`-- TrimExpand.pm
本当はこう書きたい
でもMTにダメって言われる><
というか「continuation="..."」は完全に無視される><
引数を3つ以上にすることもできます
<mt:entrytitle trim_to="10","...","xxx","yyy","zzz" />
何個増やそうが、
my ($str, $val, $ctx) = @_;
の$valに配列として渡されるので、よしなに処理したってください。