INTEGRATING SMARTY AND EZCOMPONENTS WITH THE ZEND FRAMEWORK

Integrating Smarty and ezComponents with the Zend Framework

Here is a follow-up to the first part of the little tutorial Integrating Smarty with the Zend Framework. I want to address some of the issues in the comments of the first part and add some further information on how to setup your application to use the Travello_View_Smarty class. Along the way you learn how to integrate classes of the ezComponents.

Set up a __autoload() function

Since the Zend Framework is lacking a proper configuration class until now, I integrated the configuration class of the ezComponents. With a proper __autoload() function this is no big deal. Just download the ezComponents and add the path to the ezComponents to your include_path. Then amend your __autoload() function like this:

    function __autoload($class)
{
if ('ezcBase' == $class and false == class_exists('ezcBase'))
{
require_once 'Base/src/base.php';
}
elseif ('ezc' == substr($class, 0, 3))
{
ezcBase::autoload($class);
}
else
{
Zend::loadClass($class);
}
}

Since all ezComponents classes start with "ezc" you can easily identify such classes and use them wherever you want. The first part of the if-statement makes sure that ezcBase class is autoloaded as well, so it will only be loaded when you want to use an ezComponents class. With this __autoload function you can use any ezComponent class you want to.

Use the configuration class

Using the configuration class is quite simple. I decided to use a simple .ini file for configuration and therefore I use the ezcConfigurationIniReader class. My .ini file is called 'settings.ini' and is located in the 'path/to/ini/files/' directory. The method call $reader->load() returns an ezcConfiguration object which is registered to the Zend object store.

    $reader = new ezcConfigurationIniReader();
$reader->init('path/to/ini/files/', 'settings');
$config = $reader->load();
Zend::register('config', $config);

Whenever you need to access the configuration object, you can grab it from the object store and just use it.

    $config = Zend::registry('config');
$dir = $config->getSetting('framework', 'controller_dir');

Just for illustration here is a sample ini file.

# Settings for Smarty
[smarty]
caching = true
cache_lifetime = 10
base_dir = /path/to/view
template_dir = /path/to/view/tpl
compile_dir = /path/to/view/cpl
config_dir = /path/to/view/cfg
cache_dir = /path/to/view/cch

# Settings for the Zend Framework
[framework]
application_dir = /path/to/application
controller_dir = /path/to/application/controller
model_dir = /path/to/application/model
view_dir = /path/to/application/view

Disconnect the dependency

In the first part of this tutorial there was an annoying dependency between the extended View class and the configuration object in the constructor of the View class. As a reminder here is the code from the first part:

    public function __construct($data = array())
{
parent::__construct($data);

$config = Zend::registry('config');

$this->_smarty = new Smarty();

$this->_smarty->caching = $config->getSetting('smarty', 'caching');
$this->_smarty->cache_lifetime = $config->getSetting('smarty', 'cache_lifetime');
$this->_smarty->template_dir = $config->getSetting('smarty', 'template_dir');
$this->_smarty->compile_dir = $config->getSetting('smarty', 'compile_dir');
$this->_smarty->config_dir = $config->getSetting('smarty', 'config_dir');
$this->_smarty->cache_dir = $config->getSetting('smarty', 'cache_dir');
}

I amended the constructor to accept two parameters with settings data: one for the parent class and one for Smarty.

    public function __construct($viewSettings = array(), $smartySettings = array())
{
parent::__construct($viewSettings);

$this->_smarty = new Smarty();

foreach($smartySettings as $key => $value)
{
$this->_smarty->$key = $value;
}
}

Now the instantiation of the View class will work like this.

    $viewSettings = array(
'scriptPath' => array(
$config->getSetting('framework', 'view_dir'),
$config->getSetting('smarty', 'template_dir')
)
);

$smartySettings = $config->getSettingsInGroup('smarty');

$view = new Travello_View_Smarty($viewSettings, $smartySettings);
Zend::register('view', $view);

I use two different values for the 'scriptPath' setting. The first is a global template path and the second a project specific template path. For more information on cascading template paths please look at the Zend Framework Manual.

Conclusion

With these amendments the usage of the extended View class for Smarty is getting even simpler. The integrattion of the ezComponents is also very simple, so you can pick the components from both the Zend Framework and the ezComponents without any hassle.

If I find the time I will continue my little cherry-picking with integrating Propel into the Zend Framework next time.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值