现在的位置: 首页 > 综合 > 正文

Integrating Smarty and ez Components with the Zend Framework

2013年10月14日 ⁄ 综合 ⁄ 共 8408字 ⁄ 字号 评论关闭
文章目录

Integrating Smarty and ez Components with the Zend Framework 

http://devzone.zend.com/node/view/id/156

Integrating Smarty and ez Components 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 eZ Components.

Set up a __autoload() function

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

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

Since all ez Components 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 ez Components class. With this __autoload function you can use any ez Components 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.

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

Just for illustration here is a sample ini file.

 
# Settings for Smarty
[smarty]
caching        
= true
cache_lifetime 
= 10
base_dir       
= path oview
template_dir   
= path oview pl
compile_dir    
= path oviewcpl
config_dir     
= path oviewcfg
cache_dir      
= path oviewcch

# Settings for the Zend Framework
[framework]
application_dir 
= path oapplication
controller_dir  
= path oapplicationcontroller
model_dir       
= path oapplicationmodel
view_dir        
= path oapplicationview

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 ez Components is also very simple, so you can pick the components from both the Zend Framework and the ez Components without any hassle.

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

Updated on April 18, 2006

I just corrected the misspelling of the ez Components and deleted the superfluous "and false == class_exists('ezcBase')" part of the if-statement in my __autoload() function.

Comments

Monday, April 17, 2006
__AUTOLOAD FUNCTION

7:24AM PDT · d_rethans

RE: __AUTOLOAD FUNCTION

5:38PM PDT · Ralf Eggert

Tuesday, April 18, 2006
__AUTOLOAD FUNCTION

2:48AM PDT · Ralf Eggert

Tuesday, May 2, 2006
RE: INTEGRATING SMARTY AND EZ COMPONENTS WITH THE ZEND FRAMEWORK

6:49AM PDT · paul g [unregistered]

Sunday, May 7, 2006
@PAUL

7:22PM PDT · Ralf Eggert

 

抱歉!评论已关闭.