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

Name

registerPlugin() — 動的にプラグインを登録する

説明

void registerPlugin(string type,
                    string name,
                    mixed callback,
                    bool cacheable,
                    mixed cache_attrs);

このメソッドは、スクリプト内でプラグインとして定義された関数やメソッドを登録します。 次のパラメータを使います。

  • type にはプラグインの型を定義します。使える値は "function"、"block"、"compiler" および "modifier" です。

  • name にはプラグインの名前を定義します。

  • PHP 関数のコールバック function は、次のいずれかとなります。

    • 関数名を含んだ文字列

    • array(&$object, $method) 形式の配列 (&$object はオブジェクトの参照で、 $method はメソッド名を含む文字列)

    • array($class, $method) という形式の配列 ($class はクラス名であり、 $method はクラスのメソッド名)

  • cacheable および cache_attrs は、たいていの場合は省略できます。これらの適切な使いかたについては プラグイン出力のキャッシュ制御 を参照ください。

Example 13.27. 関数プラグインの登録


<?php
$smarty->registerPlugin("function","date_now", "print_current_date");

function print_current_date($params, $smarty)
{
  if(empty($params["format"])) {
    $format = "%b %e, %Y";
  } else {
    $format = $params["format"];
  }
  return strftime($format,time());
}
?>

   

テンプレートでは次のように使います。


{date_now}

{* 別のフォーマットを使う場合 *}
{date_now format="%Y/%m/%d"}


Example 13.28. ブロック関数プラグインの登録


<?php
// 関数の宣言
function do_translation ($params, $content, $smarty, &$repeat, $template)
{
  if (isset($content)) {
    $lang = $params["lang"];
    // $content に対して何らかの変換をします
    return $translation;
  }
}

// smarty に登録します
$smarty->registerPlugin("block","translate", "do_translation");
?>

   

テンプレート側は、このようになります。


{translate lang="br"}Hello, world!{/translate}

   

Example 13.29. 修飾子プラグインの登録


<?php

// PHP の stripslashes 関数を Smarty の修飾子としてマップします
$smarty->registerPlugin("modifier","ss", "stripslashes");

?>

テンプレート側では、ss でスラッシュを除去できます。


<?php
{$var|ss}
?>


unregisterPlugin()プラグイン関数プラグインブロック関数プラグインコンパイラ関数、 そして プラグイン修飾子の作成 の節も参照ください。

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors