This is my first Zend Framework article, and I still only grasp about 5% of it, so please be constructive in your criticism!
Following on from my article on how to produce a Facebook fan page widget using valid code, I wanted to see if I could create a View Helper in Zend Framework that does the work for us.
My library is called Gamajo and so the class file below sits at library/Gamajo/View/Helper/FacebookFanPage.php.
/**
* @category Gamajo
* @package Gamajo_View
* @subpackage Helper
* @author Gary Jones
* @copyright Gamajo Tech
* @license http://gamajo.com/new-bsd-license/
* @link http://code.garyjones.co.uk/facebook-fan-page-zend-framework-view-helper/
* @since 2010-04-01
* @version 2010-04-01
*/
/**
* Facebook fan page view helper
*
* Usage:
* <code>echo $this->facebookFanPage(140949818337, array('stream'=>'0'));</code>
*/
class Gamajo_View_Helper_FacebookFanPage extends Zend_View_Helper_Abstract {
/**
* Add a Facebook Fan Page
*
* @throws Zend_View_Exception
* @param integer $fbid ID that Facebook gives to the fan page
* @param array $attribs Attributes for class name, number of connections and stream
* @return string
*/
public function facebookFanPage( $fbid, $attribs = array( ) ) {
$filter = new Zend_Filter_Digits();
$fbid = $filter->filter( $fbid );
if ( ! is_numeric( $fbid ) ) {
require_once 'Zend/View/Exception.php';
$e = new Zend_View_Exception( 'First param must be an integer' );
$e->setView( $this->view );
throw $e;
}
// Merge default values
$attribs = array_merge( array( 'connections' => 12,
'stream' => 1,
'class' => 'facebook_fans' ), $attribs );
extract( $attribs );
$xhtml = '<!--[if !IE]><!-->';
$xhtml .= $this->view->htmlObject( 'http://www.facebook.com/connect/connect.php?id=' . $fbid
. '&amp;connections=' . $connections . '&amp;stream=' . $stream, 'text/html', array( 'class' => $class ) );
$xhtml .= '<!--' . "\n" . '<!--[if IE]>-->';
$xhtml .= '';
$xhtml .= '';
return $xhtml;
}
}
As the class is within my own library, and as I have the following in my application/configs/application.ini file:
resources.view.helperPath = "Gamajo/View/Helper/" resources.view.helperPathPrefix = "Gamajo_View_Helper_"
… then as per the comments in the class file, usage in a view script or layout is as simple as:
<?= $this->facebookFanPage(140949818337, array('stream'=>'0')); ?>
I’m sure there’s probably better ways of adding this view helper, and I’d be interested to hear your comments!