CodeIgniter を使用して Smarty を統合する方法

不言
リリース: 2023-04-01 08:22:01
オリジナル
1507 人が閲覧しました

この記事では、主に Smarty と CodeIgniter を統合する方法を紹介し、Smarty3.1.27 を統合する CodeIgniter3.0.3 の手順と関連設定スキルを例とともに分析します。必要な場合は、次の例を参照してください。この記事では、CodeIgniter が Smarty を統合する方法について説明します。参考までに皆さんと共有してください。詳細は次のとおりです。

CI3.0.2 のリリース後、テンプレート クラスがまだ使いにくく、コンパイルできないように感じています。 Smarty は、一度慣れてしまうとなかなかやめられない強力な機能を持っており、また、CI のテンプレート機能の不足を補うために、それらを統合することもできます。 CI バージョン 3.0.3 と Smarty バージョン 3.1.27 を統合しています。統合プロセスについては以下で説明します。

1.smarty-3.1.27

2.smarty-3.1.27 を CI プロジェクトの application\libraries に解凍し、他のファイルを削除します。

3. 次のコードを使用して、application\libraries ディレクトリに Ci_smarty.php ファイルを作成します。

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require(APPPATH.'libraries/smarty-3.1.27/libs/Smarty.class.php');
class Ci_smarty extends Smarty {
 protected $ci;
 public function __construct()
 {
 parent::__construct();
 $this->ci = & get_instance();
 $this->ci->load->config('smarty');//加载smarty的配置文件
 $this->cache_lifetime =$this->ci->config->item('cache_lifetime');
 $this->caching = $this->ci->config->item('caching');
 $this->config_dir = $this->ci->config->item('config_dir');
 $this->template_dir = $this->ci->config->item('template_dir');
 $this->compile_dir = $this->ci->config->item('compile_dir');
 $this->cache_dir = $this->ci->config->item('cache_dir');
 $this->use_sub_dirs = $this->ci->config->item('use_sub_dirs');
 $this->left_delimiter = $this->ci->config->item('left_delimiter');
 $this->right_delimiter = $this->ci->config->item('right_delimiter');
 }
}
ログイン後にコピー

4. 次のコードを使用して、application\config ディレクトリに設定ファイル Smarty.php を作成します。コード:

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$config['cache_lifetime'] = 60;
$config['caching'] = false;
$config['template_dir'] = APPPATH .'views';
$config['compile_dir'] = APPPATH .'views/template_c';
$config['cache_dir'] = APPPATH . 'views/cache';
$config['config_dir'] = APPPATH . 'views/config';
$config['use_sub_dirs'] = false; //子目录变量(是否在缓存文件夹中生成子目录)
$config['left_delimiter'] = '{';
$config['right_delimiter'] = '}';
ログイン後にコピー

5. application\core に MY_controller.php を作成します。コードは次のとおりです:

class MY_controller extends CI_Controller {
 public function __construct() {
 parent::__construct();
 }
 public function assign($key,$val)
 {
 $this->ci_smarty->assign($key,$val);
 }
 public function display($html)
 {
 $this->ci_smarty->display($html);
 }
}
ログイン後にコピー

この時点で、構成の統合作業は完了です。設定が成功したかどうか。

7. application\controllers の Welcome.php を変更します。コードは次のとおりです。

defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends MY_controller {
 public function index()
 {
 $test='ci 3.0.3 + smarty 3.1.27 配置成功';
 $this->assign('test',$test);
 $this->display('test.html');
 }
}
ログイン後にコピー

次に、application\views に test.html ファイルを作成します。コードは次のとおりです。

{$test}
ログイン後にコピー

ブラウザのアドレス バーに次のように入力します:

http://localhost/index.php/Welcome

結果は次のように表示されます:

ci 3.0.3 + smarty 3.1.27 配置成功
ログイン後にコピー

You'終わった!

上記がこの記事の全内容です。その他の関連コンテンツについては、PHP 中国語 Web サイトをご覧ください。

関連する推奨事項:

smarty と adodb を Codeigniter に統合するためのメソッドの分析について


Symfony と CodeIgniter for PHP についてフレームワークの Nginx 書き換えルール設定


以上がCodeIgniter を使用して Smarty を統合する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!