Zend и Smarty
Чтобы использовать шаблонизатор Smarty в Zend Framework‘е можно или расширить базовый класс Zend_View_Abstract или же создать новый, реализующий интерфейс Zend_View_Interface.
Ниже привожу куски кода первого варианта:
-
class SmartyView extends Zend_View_Abstract
-
{
-
protected $_smarty;
-
{
-
$this->_smarty = new Smarty();
-
else
-
$this->_smarty->template_dir = $config[‘templateDir’];
-
else
-
$this->_smarty->compile_dir = $config[‘compileDir’];
-
$this->_smarty->config_dir = $config[‘configDir’];
-
-
$this->_smarty->plugin_dir[] = $config[‘pluginDir’];
-
-
parent::__construct($config);
-
}
-
public function getEngine()
-
{
-
return $this->_smarty;
-
}
-
public function __set($key,$val)
-
{
-
$this->_smarty->assign($key,$val);
-
}
-
public function __isset($key)
-
{
-
$var = $this->_smarty->get_template_vars($key);
-
if($var)
-
return true;
-
return false;
-
}
-
public function __unset($key)
-
{
-
$this->_smarty->clear_assign($key);
-
}
-
public function assign($spec,$value = null)
-
{
-
if($value === null)
-
$this->_smarty->assign($spec);
-
else
-
$this->_smarty->assign($spec,$value);
-
}
-
public function clearVars()
-
{
-
$this->_smarty->clear_all_assign();
-
}
-
public function display($file)
-
{
-
$this->_smarty->display($file . ‘.tpl’);
-
}
-
protected function _run()
-
{
-
$this->strictVars(true);
-
$this->_smarty->assign_by_ref(‘this’,$this);
-
$templateDirs = $this->getScriptPaths();
-
$this->_smarty->template_dir = $templateDirs[0];
-
$this->_smarty->compile_id = $templateDirs[0];
-
}
-
}
Далее в index.php или где мы там запускаем наше приложение, подключаем смарти и заносим его в реестр
-
Zend_Loader::loadClass(‘Zend_View_Abstract’);
-
include(‘Smarty/Smarty.class.php’);
-
include(‘ZendViewSmarty.php’);
-
‘compileDir’ => ‘./cache/template_c’ );
-
$smarty = new SmartyView($arrConfigSmarty);
-
$registry->set(’smarty’, $smarty);
-
/* … */
-
$frontController->setParam(‘noViewRenderer’, true);
Конфигурация смарти в данном примере хардкодирована, её обычно поднимают откудато, дальше контроллер
-
class IndexController extends Zend_Controller_Action
-
{
-
public function init()
-
{
-
$this->view = Zend_Registry::get(’smarty’);
-
}
-
public function indexAction()
-
{
-
$this->view->assign(‘title’, ‘заголовок’);
-
$this->view->display(‘index/index’);
-
}
-
}
Похожие записи:
Метки: PHP


RSS
23/04/2008 в 9:31 пп
коешо по этой теме:
Writing a CMS/Community with Smarty and the Zend Framework
http://www.prodevtips.com/2007/11/02/writing-a-cms-with-smarty-and-the-zend-framework-part-1/
26/08/2008 в 9:18 пп
есть index.php, IndexController.php and index.tpl все сделал как вы описали в index.php, и IndexController.php в index.tpl папу символов для наглядности набрал…
$this->view->display(‘index/index’);
это не выводит мне мой код из index.tpl хотя в переменной title хранятся данные - ЭзаголовокЭююю тоесть все приинклудилось и отработалло… что дальше писать в index.php для вывода всего этого на экран?