Zend & Smarty – ステップワン

It’s been a while since I updated the blog. But the things were pretty busy lately…
So finally the application’s skeleton is in place. It uses Zend + Smarty. I’m also done with
Tanaka corpus parsing. I can only say that APR is really easy to use.

Here I won’t explain in details how to integrate Zend View and Smarty. This is actually
pretty easy. You may refer a pretty old post of Ralf Eggert ‘Integrating Smarty with the Zend Framework‘ or read Quentin Zervaas’s ‘Practical Web 2.0 Applications with PHP‘.

After you nailed this most likely you’ll start thinking about something more elaborate, capable of supporting Zend_View helpers such as headScript, doctype etc. And most probably you will end up with a plugin class that maps Zend_View helpers to Smarty custom functions.

Mine code looked like this:


class KB_View_Smarty_Plugin_Functions {
    private static function callHelperFunction($tagCommand, $params, &$smarty, $useSrc = true) {
        $found = true;
        try {
            $helper = $smarty->_tpl_vars ['this']->getHelper ( $tagCommand );
        } catch ( Zend_View_Exception $e ) {
            self::$LOG->logException ( $e );
            $helper = null;
            $found = false;
        }
        
        if ($found && $helper !== null) {
            $method = (isset ( $params ['method'] )) ? $params ['method'] : - 1;
            unset ( $params ['method'] );
            $args = (isset ( $params ['args'] )) ? 
                self::parseArgs ( $params ['args'] ) : null;
            
            try {
                if ($method != - 1) {
                    if (is_array ( $args ) && $useSrc) {
                        $toReturn = $smarty->_tpl_vars ['this']->$tagCommand ()->$method ( $args ['src'] );
                    } else {
                        @$toReturn = $smarty->_tpl_vars ['this']->$tagCommand ()->$method ( $args );
                    }
                } else {
                    $toReturn = $smarty->_tpl_vars ['this']->$tagCommand ();
                }
            } catch ( Exception $e ) {
                self::$LOG->logException ( $e );
                $toReturn = "";
            }
        }
        return $toReturn;
    }
    
    public static function doctypeFunction($params, &$smarty) {
        return self::callHelperFunction ( 'doctype', $params, $smarty, false );
    }
    
    public static function headTitleFunction($params, &$smarty) {
        return self::callHelperFunction ( 'headTitle', $params, $smarty, false );
    }
    
    public static function headScriptFunction($params, &$smarty) {
        return self::callHelperFunction ( 'headScript', $params, $smarty );
    }
    
    public static function jsLocaleFunction($params, &$smarty) {
        return self::callHelperFunction ( 'jsLocale', $params, $smarty );
    }
    
    public static function headStyleFunction($params, &$smarty) {
        return self::callHelperFunction ( 'headStyle', $params, $smarty );
    }
}

Then I simply registered plugin with Smarty.

foreach ( $functions as $key => $value ) {
    $this->_view->getEngine ()->register_function ( $key, $value );
}

And this actually works. Now you can use the following syntax in your smarty templates:


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    {headTitle|upper title="index page"}
    {headLink}
    {headStyle}
    {headMeta}
    {headScript src="js/dojo/_base.js"}
    </link>
</head>

But still there is a space for optimization and code minimization. But that will be another story…

Tags: , ,

Leave a Reply