パラグラフ数をカウント(count_paragraphs)
広告
「count_paragraphs」は対象の変数に含まれるパラグラフ数をカウントします。書式は次の通りです。
{$変数名|count_paragraphs}
パラグラフの区切りは改行です。段落がいくつかあるのかを数えます。
具体的には次のように記述します。
段落数は {$var|count_paragraphs} 個です
サンプルプログラム
では簡単なサンプルプログラムを作成して試してみます。
<?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', '修飾子のテスト');
$msg=<<<EOT
こんにちは。
お元気ですか。
さようなら。
EOT;
$smarty->assign('msg', $msg);
$smarty->display('sample16-1.tpl');
?>
上記を「sample16-1.php」の名前で「(Apacheドキュメントルート)¥smarty¥modifier」に保存します。
{* Smarty modifier/sample16-1.tpl *}
<html>
<head>
<title>Smarty Test</title>
</head>
<body>
<h1>{$title}</h1>
<p>{$msg|nl2br}</p>
<p>段落数は {$msg|count_paragraphs} 個です</p>
</body>
</html>
上記を「sample16-1.tpl」の名前で「D:¥smartysample¥modifier¥templates」に保存します。
そしてブラウザから「http://localhost/smarty/modifier/sample16-1.php」へアクセスして下さい。
( Written by Tatsuo Ikura )
PHPBook