[ Smarty3.1 ] デフォルトでエスケープ処理を行うようにする

Pocket

ここでは、Smarty3.1 で新規に追加された escape_html プロパティの使用サンプルを掲載しています。個別にエスケープ処理を記述する必要がなくなりました。

スポンサーリンク

デフォルトでエスケープ処理を行うサンプル

Smarty3.1 以前では、割り当てた変数のエスケープ処理を行うには、テンプレート内で個々の変数ごとにエスケープ処理を記述するか、$smarty->default_modifiers などでエスケープを記述していました。

Smarty3.1 で、escape_html プロパティ ( public 変数 ) が追加され、デフォルトでエスケープ処理を行う設定ができるようになりました。

以下は、デフォルトでエスケープ処理を行うようにするサンプルコードになります。

require_once '/path/to/Smarty.class.php';

$smarty = new Smarty();

$smarty->template_dir = '/path/to/templates/';
$smarty->compile_dir  = '/path/to/templates_c/';
$smarty->config_dir   = '/path/to/configs/';
$smarty->cache_dir    = '/path/to/cache/';

$smarty->escape_html  = true; // デフォルトでエスケープ処理を行う

// アサイン
$smarty->assign('name','Ned');

// 出力
$smarty->display('index.tpl');
スポンサーリンク


Pocket

Leave a Comment

Your email address will not be published. Required fields are marked *