PHP - smarty2 から smarty3 へ - {php}{/php}を有効にする
- 1. 概要
- 2. {php}{/php}を有効にする
1. 概要
「smarty2」では、テンプレートファイル内に
{php}
include(PHPファイル名);
{/php}
てなことを書いて、PHP を実行させることができたのですが、「smarty3」でこれをやると
[23-Sep-2019 17:48:04 Asia/Tokyo] PHP Fatal error: Uncaught --> Smarty Compiler: Syntax error in template "file:テンプレートファイル名" on line 1 "{php}" {php}{/php} tags not allowed. Use SmartyBC to enable them <--
thrown in /usr/local/share/smarty3-php72/sysplugins/smarty_internal_templatecompilerbase.php on line 1
てな、エラーになります。
「{php}{/php}」は、お行儀がよくないので、推奨されなくなったばかりか、エラーになるのです。
もともとはこういう文は、書いていなかったのですが。事情があって、書こうとしたらエラーになっちゃったのだ。
事情があって使いたいのに、しかも、以前は許していたものが許されないのは困ったもんです。
本項は、「Smarty 3.1.13 のテンプレート内で生 php 実行 - Qiita」を参考にさせていただきました。
2. {php}{/php}を有効にする
前ページで書いている、「Smarty」クラスの定義ですが・・・。
<?php
require_once('Smarty.class.php');
class MySmarty extends Smarty
{
public function __construct ()
{
parent::__construct();
・・・
}
}
?>
の部分を
<?php
require_once('SmartyBC.class.php');
class MySmarty extends SmartyBC
{
public function __construct ()
{
parent::__construct();
$this->php_handling = Smarty::PHP_ALLOW;
・・・
}
}
?>
とするのだそうな・・・。
もしくは、クラスを使っている側で
<?php
require_once('SmartyBC.class.php');
$smarty = new SmartyBC();
$smarty->php_handling = Smarty::PHP_ALLOW;
・・・
?>
「SmartyBC」の「BC」は、「before Christ」なのかしらん?
|
|