Zend & Smarty – ステップニ

Posted in development on October 13th, 2008 by admin

In this post I will reveal the secret of really smart integration between Zend and Smarty :) Since I’m too lazy to write 10 similar functions inside the Smarty plugin, I decided to modify the Smarty compiler. And it worked well.

Making Smarty zend-aware

This step is simple – you’re just adding a function that allows you to call Zend View Helper using
call_user_func_array.


class KB_SmartyZendAware extends Smarty {
    private $_zendView                 = null;
    private $_cfg;

    public function __construct() {
        parent::__construct();
        $this->compiler_class = 'KB_SmartyZendAwareCompiler';
    }

    public function setZendView(Zend_View_Abstract $view) {
        if( $view === null ) {
            throw new KB_Exception('Zend_View cannot be null.', KB_Exception::KB_SMARTY);
        }
        $this->_zendView = $view;
    }

    public function callZendViewHelper( $name, $method, $args ) {
        if( $this->_zendView === null || ! is_string($name) || strlen($name) == 0 ) {
            return '';
        }
        $helper = $this->_zendView->getHelper($name);

        if( ! is_string($method) || strlen($method) == 0 ) {
            return call_user_func_array( array( $helper, $name ), $args);
        } else {
            return call_user_func_array( array( call_user_func( array($helper, $name) ), $method ), $args);
        }
    }
}

Read more »

Tags: , ,