子魚@NET

渤海子魚的BLOG

用户操作
[即时聊天] [发私信] [加为好友]
渤海子魚ID:ffyd2000
14849次访问,排名8277,好友24人,关注者31人。
小二
ffyd2000的文章
原创 18 篇
翻译 0 篇
转载 21 篇
评论 3 篇
渤海子魚的公告
人生就像在风中扫落叶,只是顾着清扫眼前的落叶那是不会顺利的,如果顺着风向堆积落叶的话,那就会变得很轻松了.事物不能光用眼睛看,感觉也是很重要的,仅仅拘泥于眼前的事物,忽略其他的东西也是不行的.抛下邪念,接受现状,这才是所谓的领悟
最近评论
psnccs:WoW Gold
farmer_chs:转贴
SOW觉得文章哪里有问题呢?
SOW:没有搞过PHP不要把别人写的拿来乱说
文章分类
收藏
相册
MY PHOTOS
大牛们
billy
Nosound
刘未鹏|C++的罗浮宫
小新(RSS)
木头
牛蛙
白云
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

转载 INTEGRATING SMARTY AND EZCOMPONENTS WITH THE ZEND FRAMEWORK收藏

新一篇: Zend framework简介 | 旧一篇: A Practical Tutorial Of 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.

发表于 @ 2006年04月18日 17:52:00|评论(loading...)|编辑

新一篇: Zend framework简介 | 旧一篇: A Practical Tutorial Of Zend Framework(六)

评论:没有评论。

发表评论  


登录
Csdn Blog version 3.1a
Copyright © 渤海子魚