「Php/Smarty/2から3へ移行」の版間の差分

提供: 初心者エンジニアの簡易メモ
ナビゲーションに移動 検索に移動
 
(同じ利用者による、間の13版が非表示)
1行目: 1行目:
=基本的なところはここを見る=
=基本的なところはここを見る=
http://nplll.com/archives/2010/04/smarty2_3.php
<pre>
http://nplll.com/archives/2010/04/smarty2_3.php
</pre>


=自分メモはこちら=
=自分メモはこちら=
==$smarty->template_dirがpublicからprivateへ==
==$smarty->template_dirがpublicからprivateへ==
  $smarty->setTemplateDir($template_dir); 設定
  $smarty->setTemplateDir($template_dir); // 設定
  $smarty->getTemplateDir(); 取得
  $smarty->getTemplateDir(); // 取得(arrayで取得される)
$smarty->getTemplateDir(0); // 取得(最初のpathが取得される)


==$smarty->compile_dirがpublicからprivateへ==
==$smarty->compile_dirがpublicからprivateへ==
23行目: 26行目:
  {
  {
   public function __construct() {
   public function __construct() {
    parent::__construct();
     $this->debugging = false;
     $this->debugging = false;
     $this->left_delimiter = '{{<nowiki />';
     $this->left_delimiter = '{{<nowiki />';
31行目: 35行目:
     $this->addPluginsDir(SMARTY_PLUGINS_DIR);
     $this->addPluginsDir(SMARTY_PLUGINS_DIR);
     $this->addPluginsDir(__DIR__ . '/smartyPlugins');
     $this->addPluginsDir(__DIR__ . '/smartyPlugins');
    $this->Smarty();
   }
   }
  }
  }


==phpタグを使用してコード内でphpを実行する場合==
==phpタグを使用してコード内でphpを実行する場合==
このようなエラーが発生した場合
Smarty.class.phpでなくSmartyBCを使う&PHP_ALLOWを設定する。
Fatal error: Uncaught --> Smarty: SmartyModel->SmartyBC() undefined method <-- thrown in
 
Smarty.class.phpでなくSmartyBCを使う&PHP_ALLOWを設定する
そうすることで{php} が利用できるようになります。
そうすることで{php} が利用できるようになります。
  require_once __DIR__ . '/smarty/libs/SmartyBC.class.php';
  require_once __DIR__ . '/smarty/libs/SmartyBC.class.php';
61行目: 61行目:


参考:http://www.smarty.net/forums/viewtopic.php?p=89743
参考:http://www.smarty.net/forums/viewtopic.php?p=89743
"__call($name, ~"の処理のnameが"Smarty"だったので、
$this->Smarty(); でコンストラクトを実行してたのが、間違えてcallに入ってるので、$this->Smarty();を消して、__construct()処理内に、parent::__construct();を追加。
==cacheIdについて==
smarty3だと、
日本語などが含まれるとidが削れたりするのでリクエストパラメータなどを含めてる場合は、md5などで加工する。

2026年4月8日 (水) 15:09時点における最新版

基本的なところはここを見る

http://nplll.com/archives/2010/04/smarty2_3.php

自分メモはこちら

$smarty->template_dirがpublicからprivateへ

$smarty->setTemplateDir($template_dir); // 設定
$smarty->getTemplateDir(); // 取得(arrayで取得される)
$smarty->getTemplateDir(0); // 取得(最初のpathが取得される)

$smarty->compile_dirがpublicからprivateへ

$smarty->setCompileDir(compile_dir); 追加設定
$smarty->getCompileDir(); 取得

$smarty->cache_dirがpublicからprivateへ

$smarty->setCacheDir(compile_dir); 追加設定
$smarty->getCacheDir(); 取得

$smarty->plugins_dir[]がpublicからprivateへ

$smarty->addPluginsDir($plugins_dir); 追加設定
$smarty->getPluginsDir(); 取得

カスタム設定例

class MySmarty extends Smarty
{
  public function __construct() {
    parent::__construct();
    $this->debugging = false;
    $this->left_delimiter = '{{';
    $this->right_delimiter = '}}';
    $this->setTemplateDir(__DIR__ . "/../views/templates");
    $this->setCompileDir(__DIR__ . "/../views/templates_c/");
    $this->setCacheDir(__DIR__ . "/../views/cache/");
    $this->addPluginsDir(SMARTY_PLUGINS_DIR);
    $this->addPluginsDir(__DIR__ . '/smartyPlugins');
  }
}

phpタグを使用してコード内でphpを実行する場合

Smarty.class.phpでなくSmartyBCを使う&PHP_ALLOWを設定する。 そうすることで{php} が利用できるようになります。

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

memcacheなどにエンジンを変更する場合

smarty2

$smarty->cache_handler_func = array($smartyMemcache, 'cacheHandler');

smarty3

$smartyMemcache = new Smarty_CacheResource_Memcache($cacheInstance);
$smarty->registerCacheResource('memcache', $smartyMemcache); // memcache登録
$smarty->caching_type = 'memcache'; // cache方法にmemcacheを選択

参考:https://github.com/koriym/Smarty3/blob/master/demo/plugins/cacheresource.memcache.php

Call to a member function _callExternalMethod() エラー

Call to a member function _callExternalMethod() on a non-object in /var/www/html/maia/libs/Smarty/sysplugins/smarty_internal_data.php

__constructメソッド内にparent::__cunstruct()を記載してないとこのエラーが出る。

参考:http://www.smarty.net/forums/viewtopic.php?p=89743

"__call($name, ~"の処理のnameが"Smarty"だったので、 $this->Smarty(); でコンストラクトを実行してたのが、間違えてcallに入ってるので、$this->Smarty();を消して、__construct()処理内に、parent::__construct();を追加。

cacheIdについて

smarty3だと、 日本語などが含まれるとidが削れたりするのでリクエストパラメータなどを含めてる場合は、md5などで加工する。