LoginSignup
12

More than 5 years have passed since last update.

Smarty 3.1.13 のテンプレート内で生php実行

Posted at

Smarty3系列だとデフォルトで{php}タグが使えない

ググってみたらやたらと allow_php_tag な情報ばかり見つかるので真似してみたけど、そんなプロパティ無いって怒られる。Smarty 3.1.13 の時点では allow_php_tag は obsolete になってました。

エラー例

テンプレートファイル

<html>
{php}
$a = 10;
echo $a;
{/php}
</html>

Smartyエンジン呼び出し部分

require_once('..../smarty/libs/Smarty.class.php');
$smarty = new Smarty();
....
$smarty->display('..../hoge.tpl');

エラーログ

PHP Fatal error:  Uncaught exception 'SmartyCompilerException' with message 'Syntax Error in template .... &quot;{php}&quot; unknown tag &quot;php&quot;' in /..../smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php:665

unknown tag、といわれましても。

対処

Smartyエンジン呼び出し部分を変更

Smarty ではなく SmartyBC を使い、php_handling に Smarty::PHP_ALLOW を設定。

require_once('..../smarty/libs/SmartyBC.class.php');
$smarty = new SmartyBC();
$smarty->php_handling = Smarty::PHP_ALLOW;
....
$smarty->display('..../hoge.tpl');

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12