Tak na szybko. Poniższe rozwiązanie ma przybliżyć jeden z wielu sposobów wykorzystania pakietu SMARTY na prostym przykładzie, tak więc można mieć wątpliwości co do idei OOP tam przedstawionego, ale nie o to teraz chodzi....
config.inc.php<?php
/**
* -------------------------------------------------------------
*
* Glowny plik konfiguracyjny
*
* @name config.inc.php, charset UTF-8
* @author nazihipi
* -------------------------------------------------------------
*/
define( 'APP_ROOT', 'twoja_sciezka_do_aplikacji' ); define( 'SMARTY_DIR', APP_ROOT
. '/libs/smarty/' ); define( 'TEMPLATE_DIR', APP_ROOT
. '/templates' ); define( 'COMPILE_DIR', APP_ROOT
. '/templates_cache' ); define( 'CONFIG_DIR', APP_ROOT
. '/configs' ); define( 'PLUGINS', SMARTY_DIR
. '/plugins' ); define( 'SMARTY_PLUGINS', APP_ROOT
. '/smarty_plugins' );
// Koniec config.inc.php
?>
class.Smarty.php<?php
/**
* -------------------------------------------------------------
*
* Dane lokalnych ustawien SMARTY.
*
* @name class.Smarty.php, charset UTF-8
* @author nazihipi
* -------------------------------------------------------------
*/
require_once 'config.inc.php';
require_once SMARTY_DIR . 'Smarty.class.php';
class Application extends Smarty {
/**
* Ustaw sciezki do katalogow SMARTY
*
* @access public
*/
function __construct() {
$this->Smarty();
$this->template_dir = TEMPLATE_DIR;
$this->compile_dir = COMPILE_DIR;
$this->config_dir = CONFIG_DIR;
$this->plugins_dir[0] = PLUGINS;
$this->plugins_dir[1] = SMARTY_PLUGINS;
}
}
// Koniec class.Smarty.php
?>
smarty_plugins/function.home.php (nazewnictwo:
function.twoja_nazwa.php)
<?php
/**
* -------------------------------------------------------------
*
* Obsluga zadan na stronie glownej
*
* @name function.home.php, charset UTF-8
* @package smarty
* @subpackage smarty_plugins
* @author nazihipi
* -------------------------------------------------------------
*/
// (nazewnictwo: smarty_function_TwojaNazwa)
function smarty_function_home( $parms, $smarty ) {
$home = new home();
$home->run();
$smarty->assign( $parms['assign'], $home );
}
class home {
// Zmienne skladow dostepne z poziomu SMARTY
public $mMessage = true;
public $mResult;
// Prywatne zmienne skladowe
private $_mGetData;
/**
* Uzyskaj dostep do poszczegolnych metod, klas
*
* @access public
*/
function __construct() {
// Utworz obiekt, ktory umozliwi pobranie danych z bazy
// (Klasa Get oczywiście zaimplementowana i załączona np. w index.php)
$_mGetData = new Get();
}
/**
* Glowna metoda
*
* @access public
* @return array
*/
public function run() {
// np. Sprawdz, czy user obsluguje cookies
if( isset( $_COOKIE['sid'] ) ) { $this->mMessage = false;
}
// Pobierz newsy z bazy
$this->mResult = $this->_mGetData->getNews();
}
}
// Koniec function.home.php
?>
templates/home.tpl{* ---------------------------------- *}
{* Glowny plik szablonu *}
{* ---------------------------------- *}
{ home assign = "home" } {* nazewnictwo: (NazwaFunkcji assign = "NazwaFunkcji") *}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <link href="templates_data/css/main.css" type="text/css" rel="stylesheet" media="screen" />
{strip}
{ if $home->mMessage }
<div id="warning">Twoja przeglądarka nie obsługuje cookies!
</div> { /if }
{ section name=news loop=$home->mGetData }
<div class="n_title">{ $home->mGetData[news].title }
</div> <div class="n_text">{ $home->mGetData[news].text }
</div> <div class="n_author">{ $home->mGetData[news].author }
</div> { /section }
{/strip}
{* Koniec home.tpl *}
index.php<?php
/**
* -------------------------------------------------------------
* Glowny plik apliklacji
*
* @name index.php, charset UTF-8
* @author nazihipi
* -------------------------------------------------------------
*/
require_once 'config.inc.php';
require_once 'class.Smarty.php';
require_once 'class.Get.php';
$Application = new Application();
$Application->display('home.tpl');
// Koniec: index.php
?>
Takie rozwiązanie pozwala na bardzo proste dodawanie kolejnych modułów, ścisłe segregowania zadań dla każdej części aplikacji etc.
Mam nadzieję że się przyda...