今さらSmartyの入門vol.6~修飾子~
日付の値を「YYYY/MM/DD」、または「YYYY年MM月DD日」と表示させたい場合など、元のデータから加工して表示させたい場合がある。
値の加工はphpの関数で行うことはできるが、表示させるための処理をphp側で行うことは、php+Smartyでの開発にはなじまない。
もし表示処理をphp側で行っていたら、テンプレート側で表示させたい形式を変更したい場合に、php側の表示処理をプログラマーがいちいち修正しなければならなくなってしまう。またphp側でデータ加工をしてしまうと、その分だけテンプレート変数を準備しなければならなくなってしまう。
php側では生のデータをテンプレート変数に格納し、それをどのように表示させるかはテンプレート側に委ねることでスクリプトの冗長化を防ぐことにつながる。
そこで「修飾子」を用いて、テンプレート変数としてテンプレート側に渡されたデータを加工する。
まずはファイルの準備。
■phpファイル 格納ディレクトリ:/htdocs/直下 ファイル名:modifier.php
■テンプレートファイル 格納ディレクトリ:/php_libs/smarty/templates/ ファイル名:modifier.tpl
modifier.php [php] <?php // Smarty派生クラスを読み込む require_once("./MySmarty.class.php");
$smarty = new MySmarty(); [/php]
modifier.tpl [html] <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>修飾子</title> </head> <body>
</body> </html> [/html]
capitalize
単語の頭文字を大文字に変換 [html] <p>i have a pen.→{"i have a pen."|capitalize}</p> [/html]
実行結果 [html] <p>i have a pen.→I Have A Pen.</p> [/html]
cat
文字列を連結 modifier.php [php] $smarty->assign("testText", "I have a pen."); [/php]
modifier.tpl [html] <p>{$testText|cat:"I have an apple."}</p> [/html]
実行結果 [html] <p>I have a pen.I have an apple.</p> [/html]
lower
小文字に変換 modifier.tpl [html] <p>I Have A Pen.→{"i have a pen."|lower}</p> [/html]
実行結果 [html] <p>I Have A Pen.→i have a pen.</p> [/html]
upper
大文字に変換 modifier.tpl [html] <p>i have a pen.→{"i have a pen."|upper}</p> [/html]
実行結果 [html] <p>i have a pen.→I HAVE A PEN.</p> [/html]
regex_replace
正規表現を使用して特定部分の文字列を置換する
replace
特定部分の文字列を置換する modifier.php [php] $smarty->assign("regex", "トップページはhttp://hitori.sharoushi.jpn.com/"); [/php]
modifier.tpl [html] <p>{$regex|replace:"トップ":"ホーム"}</p> [/html]
実行結果 [html] <p>ホームページはhttp://hitori.sharoushi.jpn.com/
[/html]date_format
日付や日時のデータを整形する modifier.tpl [html] <p>{$smarty.now|date_format:"%Y年 %m月 %d日"}</p> [/html]
実行結果 [html] <p>2017年 01朁E28日</p> [/html]
上記のように文字化けする場合はテンプレートの記述を下記のように変える。 [html] <p>{$smarty.now|date_format:"%Y年%m月%d日"}</p> [/html]
実行結果 [html] <p>2017年01月28日</p> [/html] ※ブラウザ上は「2017年01月28日」と表示されている。
string_format
指定されたフォーマット文字列で整形した形で出力
%d:引数を整数として扱い、10進数として表示 %s:引数を文字列として表示 %f:引数を浮動小数点として表示
modifier.tpl [html] <p>{"3.141592"|string_format:"%d"}</p> <p>{"3.141592"|string_format:"%s"}</p> <p>{"3.141592"|string_format:"%.2f"}</p> [/html]
実行結果 [html] <p>3</p> <p>3.141592</p> <p>3.14</p> [/html]
default
変数が空の場合、デフォルト値を定義する modifier.php [php] // 一部の変数に値を格納 $smarty->assign("data01", "テストデータ01"); $smarty->assign("data02", "テストデータ02"); $smarty->assign("data03", "テストデータ03"); [/php]
modifier.tpl [html] <table border="1"> <tr> <td>{$data01|default:" "}</td> <td>{$data02|default:" "}</td> <td>{$data03|default:" "}</td> <td>{$data04|default:" "}</td> </tr> <tr> <td>{$data05|default:" "}</td> <td>{$data06|default:" "}</td> <td>{$data07|default:" "}</td> <td>{$data08|default:" "}</td> </tr> </table> [/html]
実行結果 [html] <table border="1"> <tr> <td>テストデータ01</td> <td>テストデータ02</td> <td>テストデータ03</td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> <td> </td> </tr> </table> [/html]
strip
連続した空白文字を取り除き、半角スペース1つに置き換える modifier.tpl [html] <p>わ た し は か も め→{"わ た し は か も め"|strip}</p> [/html]
実行結果 [html] <p>わ た し は か も め→わ た し は か も め</p> [/html]
strip_tags
HTMLタグを無効化 modifier.tpl [html] <p><h1>見出し<h1>→{"<h1>見出し</h1>"|strip_tags}</p> [/html]
実行結果 [html] <p><h1>見出し<h1>→ 見出し </p> [/html]
nl2br
改行文字を<br>に置き換える modifier.tpl [html] <p>1行目。 2行目。 3行目。 4行目。 5行目。 →{"1行目。 2行目。 3行目。 4行目。 5行目。"|nl2br} [/html]
実行結果 [html] <p>1行目。 2行目。 3行目。 4行目。 5行目。 →1行目。<br /> 2行目。<br /> 3行目。<br /> 4行目。<br /> 5行目。</p> [/html]
escape
HTML/URLエスケープを行う modifier.php [php] $smarty->assign("alert", "<script>alert('JavaScriptのアラート');</script>"); [/php]
modifier.tpl [html] <p>{$alert|escape:"html"|escape:"javascript"}</p> [/html]
実行結果 [html] <p><script>alert('JavaScriptのアラート');</script></p> [/html]
spacify
文字間にスペースを入れる modifier.tpl [html] <p>わたしはかもめ→{"わたしはかもめ"|spacify}</p> [/html]
実行結果 [html] <p>わたしはかもめ→わ た し は か も め</p> [/html]
truncate
文字列を指定桁で切り捨てる ※バイト単位での指定のため、2バイト文字の日本語は文字化けする可能性あり modifier.php [php] $smarty->assign("japanese", "世の人の心まどはす事、色欲にはしかず。人の心はおろかなるものかな。匂ひなどはかりのものなるに、しばらく衣裳に薫物(たきもの)すと知りながら、えならぬ匂ひには、必ずときめきするものなり。
久米の仙人の、物洗ふ女の脛(はぎ)の白きを見て、通(つう)を失ひけんは、誠に手足・はだへなどのきよらに、肥えあぶらづきたらんは、外の色ならねば、さもあらんかし。"); [/php]
modifier.tpl [html] <p>{$japanese|truncate:40:"...":true}</p> [/html]
「true」は切り捨てを単語の境界で行うか否か。デフォルトはfalse。
実行結果 [html] <p>世の人の心まどはす事、色欲にはしかず。人の心はおろかなるものかな。匂ひなど...</p> [/html]
wordwrap
文字列を指定桁で折り返す modifier.tpl [html] <p>{$japanese|wordwrap:30:"<br>":true}</p> [/html]
実行結果 [html] <p>世の人の心まどはす事、色欲にはしかず。人の心はおろかなるもの<br>かな。匂ひなどはかりのものなるに、しばらく衣裳に薫物(たきも<br>の)すと知りながら、えならぬ匂ひには、必ずときめきするものな<br>り。
久米の仙人の、物洗ふ女の脛(はぎ)の白きを見て、通(つう)を<br>失ひけんは、誠に手足・はだへなどのきよらに、肥えあぶらづきた<br>らんは、外の色ならねば、さもあらんかし。</p> [/html]
バイト単位での指定のため、1バイト文字を2バイト文字が混在すると改行前後が文字化けする可能性がある。
indent
各行で文字列をインデントする modifier.tpl [html] {$japanese|nl2br|indent:2:" "} [/html]
実行結果 [html] 世の人の心まどはす事、色欲にはしかず。人の心はおろかなるものかな。匂ひなどはかりのものなるに、しばらく衣裳に薫物(たきもの)すと知りながら、えならぬ匂ひには、必ずときめきするものなり。 久米の仙人の、物洗ふ女の脛(はぎ)の白きを見て、通(つう)を失ひけんは、誠に手足・はだへなどのきよらに、肥えあぶらづきたらんは、外の色ならねば、さもあらんかし。 [/html]
count_characters
modifier.tpl [html] <p>abcdefgの文字数:{"abcdefg"|count_characters}</p> [/html]
実行結果 [html] <p>abcdefgの文字数:7</p> [/html]
count_paragraphs
段落数を数える modifier.php [php] $smarty->assign("lorem", "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); [/php]
modifier.tpl [html] <p>{$lorem|count_paragraphs}</p> [/html]
実行結果 [html] <p>3</p> [/html]
count_sentences
センテンスの数を数える modifier.tpl [html] <p>{$lorem|count_sentences}</p> [/html]
実行結果 [html] <p>4</p> [/html]
count_words
単語数を数える modifier.tpl [html] <p>{$lorem|count_words}</p> [/html]
実行結果 [html] <p>69</p> [/html]