Main Contents

[PHP] CodeIgniter 2 et Smarty 3

mai 1, 2013

Aller un petit tuto d’installation d’un framework très connu Code Igniter et d’un moteur de template Smarty 3.
On va voir la configuration ultra simple de ces 2 outils.

Première chose on télécharge les 2 softs.
http://ellislab.com/codeigniter
http://www.smarty.net

Ensuite on décompresse le tout.
Pour CI on va tout de suite créer un htaccess pour accéder aux Controller sans appeler le index.php

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

Du coup on peut maintenant appeler un un controller comme http://mon-site/hello ou hello.php est un controller qui contient une méthode index.

Dans le dossier librairies de CI vous aller créer un dossier smarty et y copier le dossier libs de Smarty.
Il faut créer aussi un fichier sans le dossier librairies, smarty.php avec ce code :

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
require_once(APPPATH.'libraries/smarty/libs/Smarty.class.php');
 
class CI_Smarty extends Smarty {
 
    function __construct()
    {
        date_default_timezone_set('America/Phoenix');
 
        parent::__construct();
        $this->setTemplateDir(APPPATH.'views/templates');
        $this->setCompileDir(APPPATH.'views/compiled');
        $this->setConfigDir(APPPATH.'libraries/smarty/configs');
        $this->setCacheDir(APPPATH.'libraries/smarty/cache');
 
        $this->assign( 'APPPATH', APPPATH );
        $this->assign( 'BASEPATH', BASEPATH );
        // $this->caching = Smarty::CACHING_LIFETIME_CURRENT; // Does something <img src="http://searchdaily.net/wp-includes/images/smilies/icon_smile.gif" alt="icon smile CodeIgniter 2 Smarty 3 integration" class="wp-smiley" title="CodeIgniter 2 Smarty 3 integration"> 
        if ( method_exists( $this, 'assignByRef') )
        {
            $ci =& get_instance();
            $this->assignByRef("ci", $ci);
        }
        $this->force_compile = 1;
        $this->caching = true;
        $this->cache_lifetime = 120;
 
        //log_message('debug', "Smarty Class Initialized");
    }
 
    function view($template_name) {
        if (strpos($template_name, '.') === FALSE && strpos($template_name, ':') === FALSE) {
            $template_name .= '.tpl';
        }
        parent::display($template_name);
    }
 
}

Ne pas oublier de créer les dossiers views/templates, views/compiled, libraries/smarty/configs,libraries/smarty/cache.
Vous pouvez décider de mettre tout ça ailleurs, pensez juste à modifier le smarty.php.

Ensuite, il ne reste plus qu’à rajouter la classe dans l’autoload.php qui dans le dossier config

$autoload['libraries'] = array('database', 'session', 'pagination', 'smarty');

Et voilà on peut tester.
Créer un template dans views/template/index.tpl avec comme code par exemple

<html>
<head>
<title>Test Smarty</title>
</head>
<body>
Mon nom est {$name}.
</body>
</html>

Et dans votre contrôleur TestSmarty par exemple.

<?php 
 
class TestSmarty extends CI_Controller {
 
    function __construct() {
        parent::__construct();
    }
 
    function index() {
        $this->smarty->assign("name","Jérôme Kraft");
        $this->smarty->view('index');
    }
}

Catégorie(s): Développement, Tutorial, Web | Comments (0)

Leave a comment