ラベル smarty の投稿を表示しています。 すべての投稿を表示
ラベル smarty の投稿を表示しています。 すべての投稿を表示

2012年1月16日月曜日

smartyで文字列加工

修飾子

変数が空の場合のデフォルト値

{"test1"|default:"????"}

{""|default:"????"}


エスケープ

{""}

{"''"|escape}

htmlタグの削除

{"test"|strip_tags}

時間

{$smarty.now}
{$smarty.now+60*60*24|date_format:"%w"}

{$smarty.now|date_format:"%Y年%m月%d日%h時%M分%S秒"}

改行文字を変換
{$data|nl2br}
スペース
を変換


{$data|strip:""}

テンプレート内で変数を生成

{assign var="num" value="99999"}
テンプレート内の値は{$num}

smarty オブジェクトで操作

require_once('MySmarty.php');
require_once('user_bean.php');

$uses=array();
$uses[] = new user_bean(1,"test_1","2011/01/01");
$uses[] = new user_bean(2,"test_2","2010/01/01");
$uses[] = new user_bean(3,"test_3","2009/01/01");
           
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('uses',$uses);
$mysmarty->display('index.tpl');
?>
//beanクラス
<?php
class user_bean{
    //プロパティ
    public $name;
    public $id;
    public $birth;
    //メソッド
    function __construct($id,$name,$birth){
        $this->id = $id;
        $this->name = $name;
        $this->birth = $birth;
    }
    public function getage(){
        return floor((time()-strtotime($this->birth))/(60*60*24*365));
    }
}
?>

//テンプレート
{foreach from=$uses item=use}
    <p>id {$use->id}</p>
    <p>名前 {$use->name}</p>
    <p>歳 {$use->getage()}</p>
{/foreach}

Smarty 連想配列

//プログラム
<?php
require_once('MySmarty.php');
require_once('DB.php');


$dbh = DB::connect('mysql://root:mysql@127.0.0.1/test');

$data = array(    "id"    =>    1,
                "shimei"=>array("ue"=>"yamada",
                                "shita"=>"taro")
              );               
$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('data',$data);
$mysmarty->display('index.tpl');
?>
//テンプレート
<h1>{$msg}</h1>
{$data.id}<br />
{$data.shimei.ue}<br />
{$data.shimei.shita}<br />

//表示

hello MySmarty

1
yamada
taro

Smarty DBからレコードを取り出し、テンプレート表示 テスト

mysql> insert into test_1(id,name) values(1,'test'),(2,'test2');
Query OK, 1 row affected (0.05 sec)

//レコードの確認
mysql> select * from test_1;
+------+-------+
| id   | name  |
+------+-------+
|    1 | test  |
|    2 | test2 |


//プログラム
<?php
$dbh = DB::connect('mysql://root:mysql@127.0.0.1/test');

//sql文の作成
$sql = $dbh->prepare("SELECT * FROM test_1");
$data = array();
$rs = $dbh->execute($sql);
//配列へ挿入
while($row = $rs->fetchRow(DB_FETCHMODE_ASSOC)){
    $data[]=array("id" => $row['id'],"name"    => $row['name']);
}

$mysmarty = new MySmarty();
$mysmarty->assign('msg','hello MySmarty');
$mysmarty->assign('data',$data);
$mysmarty->display('index.tpl');
?>
//テンプレート部分

<h1>{$msg}</h1>
{foreach from=$data item=datum}
    <p>{$datum.id}</p>
    <p>{$datum.name}</p>
{/foreach}



smarty 入門

/*************************************************/
/*        仕事でSmartyを使うんでおさらい     */
/************************************************/


/*-----------ダウンロード---------*/
「http://www.smarty.net/download」より「Smarty 2.6.26」をダウンロード

/*-----------インストール----------*/
「Smarty 2.6.26」を展開し「Smarty」とリネイムし、
今回は「Windows」+「Ampps」環境なので「C:\Ampps\Smarty」に配置する。

/*------------php.iniの設定----------*/
//下記を編集
include_path = ".;C:\ampps\php\includes;C:\Ampps\Smarty\lib"

/*------------ディレクトリの配置----------------------*/
/*下記のように配置します。*/
smarty_sample-
              |-test_app_1
                    |-templates     //テンプレートファイル設置
                    |-templates_c   //コンパイルしたテンプレート設置
                    |-configs       //コンフィグファイル設置
                    |-cache         //キャッシュ
                   
                   
                   
                   
/*--------------テンプレートの作成---------------------------------*/
下記のhtmlソースを「templates_c\index.tpl」に配置、

{*----   テストテンプレート   ----*}

<html>
<head>
<title>Test_1</title>
</head>
<body>

<h1>{$msg}</h1>

</body>
</html>

/*--------------プログラムの作成---------------------------------*/

/*下記を「C:\Ampps\www\smarty_sample\test_app_1」に配置*/
<?php

{*require_once('C:/Ampps/Smarty/libs/Smarty.class.php');*}
require_once('marty.class.php');

$smarty = new Smarty();

$smarty->template_dir = 'C:\Ampps\smarty_sample\test_app_1/templates/';
$smarty->compile_dir  = 'C:\Ampps\smarty_sample\test_app_1/templates_c/';
$smarty->config_dir   = 'C:\Ampps\smarty_sample\test_app_1/configs/';
$smarty->cache_dir    = 'C:\Ampps\smarty_sample\test_app_1/cache/';

$smarty->assign('msg','Hello World!');
$smarty->display('index.tpl');

?>