Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

修飾子プラグイン

修飾子プラグイン は、テンプレートの変数が表示される前または他のコンテンツに使用される前に適用される関数です。

mixed smarty_modifier_name( $value,  
  $param1);  
mixed $value;
[mixed $param1, ...];
 

修飾子プラグインへの第1パラメータは、この修飾子によって影響を受ける値です。 残りのパラメータはどのような動作が行われるかによって任意です。

修飾子プラグインは処理の結果を 返す 必要があります。

Example 16.3. シンプルな修飾子プラグイン

このプラグインは、基本的に組み込みの PHP 関数の名前を変えただけのものです。 追加のパラメータはありません。


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     modifier.capitalize.php
 * Type:     modifier
 * Name:     capitalize
 * Purpose:  文字列の各単語の最初の文字を大文字にする
 * -------------------------------------------------------------
 */
function smarty_modifier_capitalize($string)
{
    return ucwords($string);
}
?>


Example 16.4. 更に複雑な修飾子プラグイン


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     modifier.truncate.php
 * Type:     modifier
 * Name:     truncate
 * Purpose:  文字列をある長さで切り捨てます。
 *           単語の真ん中で分割させたり、終端に文字列 $etc
 *           を追加したりすることもできます。
 * -------------------------------------------------------------
 */
function smarty_modifier_truncate($string, $length = 80, $etc = '...',
                                  $break_words = false)
{
    if ($length == 0)
        return '';

    if (strlen($string) > $length) {
        $length -= strlen($etc);
        $fragment = substr($string, 0, $length+1);
        if ($break_words)
            $fragment = substr($fragment, 0, -1);
        else
            $fragment = preg_replace('/\s+(\S+)?$/', '', $fragment);
        return $fragment.$etc;
    } else
        return $string;
}
?>

     

register_modifier() および unregister_modifier() も参照してください。

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors