先頭文字を大文字へ変換(capitalize)
「capitalize」は変数に格納されている半角英文字の単語について先頭文字を大文字へ変換します。書式は次の通りです。
{$変数名|capitalize}
先頭の文字だけが対象となりますので、先頭文字以外が小文字であっても大文字であっても変化しません。また変数内に複数の単語が含まれている場合は全ての単語の先頭文字が対象となります。
なおデフォルトでは数字が含まれる単語は変換されません(例えばoracle11とかweb2.0とかです)。そこで数字が含まれる場合も変換対象にする場合はパラメータに「true」を指定します。
{$変数名|capitalize:true}
パラメータを省略した場合は「false」を指定した場合と同じです。
具体的には次のように記述します。
台詞は {$msg|capitalize} だった
サンプルプログラム
では簡単なサンプルプログラムを作成して試してみます。
<?php require_once('Smarty.class.php'); $smarty = new Smarty(); $smarty->template_dir = 'd:/smartysample/modifier/templates/'; $smarty->compile_dir = 'd:/smartysample/modifier/templates_c/'; $smarty->config_dir = 'd:/smartysample/modifier/configs/'; $smarty->cache_dir = 'd:/smartysample/modifier/cache/'; $smarty->assign('title', '修飾子のテスト'); $smarty->assign('text', 'created by SMARTY and php5'); $smarty->display('sample3-1.tpl'); ?>
上記を「sample3-1.php」の名前で「(Apacheドキュメントルート)¥smarty¥modifier」に保存します。
{* Smarty modifier/sample3-1.tpl *} <html> <head> <title>Smarty Test</title> </head> <body> <h1>{$title}</h1> <p>{$text}</p> <p>{$text|capitalize}</p> <p>{$text|capitalize:true}</p> </body> </html>
上記を「sample3-1.tpl」の名前で「D:¥smartysample¥modifier¥templates」に保存します。
そしてブラウザから「http://localhost/smarty/modifier/sample3-1.php」へアクセスして下さい。
1番上がオリジナルの値、2番目がパラメータを省略した場合、3番目がパラメータに「true」を設定した場合です。
( Written by Tatsuo Ikura )