Creating Filler Content with Zend Framework

Need filler content for your Zend Framework (ZF) application? Most clients never give the developer copy up front so it’s always hard show what the site is to look like without some random text. At the beginning of a new application when you are mocking up the html and layout, I like to use static views. I basically create a full static site with Lorem Ipsum content. I hated copying text over and over so I decided to create a quick view helper that will generate the content for you on the fly. You can find the view helper on github. Currently there are four different ways to generate the filler content.

  1. Paragraphs
  2. Words
  3. Bytes
  4. and Lists

You can find more information on optional parameters and how to use the view helper at github.

Custom Zend Filter: Excerpt

So you are familiar with the WordPress template tag called the_excert() right? If not the tag takes a post and trims it up to display 55 or so words and then adds ellipses to the end. I find this very useful when you just want to display a teaser. In the current Zend Framework project then I am working on for a client I needed just this. So I had to create the custom filter which is really easy. You can find a vast amount of info for Zend Filters here.

Custom Filter – Excerpt

// To call the filter use
$excerpt = new My_Filter_Excerpt();
$excerpt->filter($str); //$str is the string of text

//Put this in your library
class My_Filter_Excerpt implements Zend_Filter_Interface
{
	protected $_wordCount = 55;

	public function filter($data)
	{
		$sentance = explode(" ", $data);
		if (count($sentance) > $this->_wordCount) {
			$sentance = array_splice($sentance, 0, $this->_wordCount);
			$ellipsis = "…";
		}
		$excerpt = implode(" ", $sentance);
		echo $excerpt . $ellipsis;
	}
}

Gravatar Zend Framework View Helper

I have worked on a Zend Framework these past couple of days and needed the ability to display a users avatar. This is easy by using Automattic’s gravatar project. The easiest way to add a users avatar is to use a (this) view helper. Basically in your view you just pass the user’s email address and a desired size and your all set. Make sure in the view helper you specify the default image if the user doesn’t have an avatar to display.

You can download the source here. Comments and improvements are always welcome.

<?php
/**
 *
 * @author Shaun Farrell
 * @version 1.0
 */
require_once 'Zend/View/Interface.php';

/**
 * Gravatar helper
 *
 * @uses viewHelper Zend_View_Helper
 */
class Zend_View_Helper_Gravatar {

	/**
	 * @var Zend_View_Interface
	 */
	public $view;
	/**
	 *
	 * @var unknown_type
	 */
	protected $_gravatarUrl = "http://www.gravatar.com/avatar.php?";
	/**
	 *
	 * @var unknown_type
	 */
	protected $_defaultImage = "http://everquest.allakhazam.com/pgfx/item_576.png";

	/**
	 *
	 * @param $emailAddress
	 * @param $size
	 * @param $imgAlt
	 */
	public function gravatar($emailAddress, $size = "40", $imgAlt = "")
	{
		$gravatarId = "gravatar_id=" . md5(strtolower(trim($emailAddress)));
		$url = $this->_gravatarUrl . $gravatarId . "&default=" . urlencode($this->_defaultImage) .
			"&size=" . $size;

		echo "<img src=\"$url\" alt=\"$imgAlt\" />";
		return null;
	}

	/**
	 * Sets the view field
	 * @param $view Zend_View_Interface
	 */
	public function setView(Zend_View_Interface $view) {
		$this->view = $view;
	}
}

Zend_Db Firebug Profiler via .ini File

So a couple of days ago I posted a way to get the DB profiler working via your bootstrap but an easier way to get things working is via your configuration .ini file.

Once again you need Firefox, FireBug, and FirePHP. Then in your .ini file add the following lines;

resources.db.isdefaulttableadapter = true
resources.db.params.profiler.enabled = true
resources.db.params.profiler.class = Zend_Db_Profiler_Firebug

The best part about this is that you can add this code only to load based on the environment set in your .htaccess or index.php file in your public folder.

Getting Zend_Db Firebug Profiler Working with Zend_Application

When developing web applications I normally run into some SQL problems and want to see what the SQL code is doing on the backend. To do this one would normally just print the SQL statement to the screen during execution. However, if you use Zend Framework you are in luck. You can profile your database calls with Firebug and it’s really easy. Here is what you will need to start.

  1. Running Zend Framework (1.8.4 or above using Zend_Application)
  2. Mozilla Firefox Browser – You should be using this anyways!
  3. The latest version of Firebug – http://getfirebug.com/
  4. The lastest version of FirePHP – http://www.firephp.org/

Once those firefox add-ons have been installed your ready to add in some code.

In your bootstrap.php file add

    /**
     * Setup the database profiling
     */
    protected function _initDbProfiler()
    {
        $this->bootstrap('db');
        $profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
        $profiler->setEnabled(true);
        $this->getPluginResource('db')->getDbAdapter()
             ->setProfiler($profiler);
    }

With this code added all your SQL statements that are executed will show up in the console window of FireBug. If it’s not working make sure that you have the net console enabled! profiling will not work if net is not enabled.

More information about Zend_Db_Profiler can be found at http://framework.zend.com/manual/en/zend.db.profiler.html