Smarty 3での{php}タグ使用時に発生するエラーの対処法

Smarty3から、{php}タグはデフォルトでは禁止されております。

エラー内容

デフォルト状態で{php}タグを使用すると、下記のようなエラーが発生します。

Fatal error: Uncaught exception 'SmartyException' with message '{php} is deprecated, set allow_php_tag = true to enable' in ~/Smarty/libs/plugins/block.php.php:21 Stack trace: #0 ~.file.~.tpl.php(21): smarty_block_php(Array, NULL, Object(Smarty_Internal_Template), true) #1 ~/Smarty/libs/sysplugins/smarty_internal_template.php(432): include('~...') #2 ~/Smarty/libs/sysplugins/smarty_internal_template.php(567): Smarty_Internal_Template->renderTemplate() #3 ~.file.~.tpl.php(22): Smarty_Internal_Template->getRenderedTemplate() #4 ~/Smarty/libs/sysplugins/smarty_internal_template.php(432): include('~...') #5 ~ in ~/Smarty/libs/plugins/block.php.php on line 21

{php}タグを使うことは非推奨となってますので使わないに越したことはないとは思いますが、どうしても使わなければならない場面もあるかと思いますので、その対処法をここに書き留めておきます。

対処方法

対処法は簡単で、ソースコードに「$smarty->allow_php_tag = true;」を追加するだけです。

例:
$smarty->allow_php_tag = true;
$smarty->assign('test', 'テスト');
$smarty->display('test.tpl');

Smarty.class.phpを直接いじるという方法もありますが、ここでは安全かつシンプルな対処法を紹介させていただきました。
困っている方のお役に立てれば何よりです。

 
    このエントリーをはてなブックマークに追加  

コメント: 3

 
  • つい先日からSmartyを使い出したところです。
    こちらの記事で助かりました。ありがとうございました。

     
     
     
    • お役に立てて何よりです。

      こうしたコメントがあると、ブログやってて良かったなって思えます。
      わざわざコメント残していただきこちらこそありがとうございました。

       
  • […] = true; を記述。 Smarty 3での{php}タグ使用時に発生するエラーの対 処法 カテゴリー: smarty   パーマリンク ← […]

     
     
     
  • Leave a Reply to 神戸のアキラ