Smartyの最近のブログ記事

register_modifier()

register_modifier() -- 変数の修飾子プラグインを動的に登録します。

http://www.smarty.net/manual/ja/api.register.modifier.php

register_modifier()を使って、「number_format2」を作成します拡張します。

 

こんな感じ。

// number_formatを拡張したnumber_format2を作成する。
// 3桁ごとの区切りで、マイナスの場合は、色を赤色とする。
$smarty->register_modifier('number_format2', 'number_format2');
function number_format2($value,$value2){
 if($value >= 0){
  // プラス
  $ret = number_format($value,$value2);
 }else{
  // マイナス
  $ret = sprintf("<font  color=\"#FF0000\">%s</font>",number_format($value,$value2));
 }
 return $ret;
}</p>

 

これでテンプレート中で、

{$hogehoge|number_format2:2}

のような感じで使える。

PHPのテンプレートエンジンといえば、Smartyです。

Smartyを使うときの作法として、自分では次のように継承クラスを必ず用意するようにしています。

xxxxSmarty.class.php(xxxは作成するSystem名)


<?php
require_once("Smarty.class.php"); // smarty.class.phpの指定。環境によって異なります。
class xxxxSmarty extends Smarty{ // Smartyクラスを継承したxxxxSmaryクラスを定義します。
public function __construct(){    // __construct()はPH5以上です。
$this->Smarty();
$this->left_delimiter = "{!";    // xoopsと同じにする。(xoopsに慣れてるので)
$this->right_delimiter = "}";
$this->template_dir = "xxxxxxxx/templates";
$this->compile_dir = "xxxxxxxx/templates_c";
}
}
?>


 テンプレート定義などソース毎に定義するのは面倒ですから、これで使うときは、
require_once("xxxxSmarty.class.php");
と自作クラスだけ呼ぶようにします。

$hoge = new xxxxSmarty();

__constructにより、newされたときに自動で実行されますので、再定義の必要がありません。

Smartyで行単位に色を変更して表示する方法。

 下記。

   
   
   
   
   

cycleを使います。

{cycle values="#333300,#cccc00"}
valuesの値(カンマ区切り)を交互(順に)出力されるものです。

<table>
####smartyのループ処理開始
  <tr bgcolor="{cycle values="#333300,#cccc00"}">

 http://www.phppro.jp/phpmanual/smarty/language.function.cycle.html

Smartyでの3桁区切りでカンマ[,]を表示する方法。

$var = 1234567890;

という値がセットされているとき。

{$var|number_format} 

こうすると

1,234,567,890

こうなります。

 

このアーカイブについて

このページには、過去に書かれたブログ記事のうちSmartyカテゴリに属しているものが含まれています。

前のカテゴリはethnaです。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

ウェブページ

Powered by Movable Type 5.04

にほんブログ村

にほんブログ村 IT技術ブログへ
にほんブログ村