Zend Framework View Helper to Lowercase Titles

When working on a web app, I often find myself with my local development version open in one browser window with the live version open in another. To allow me quickly distinguish one from the other, I like to tweak the rendering in the development version by converting the title to lower case.

Since I have recently begun using Zend Framework for much of my development, I wrote a little view helper to handle it.


class App_View_Helper_HeadTitle extends Zend_View_Helper_HeadTitle
{
    public function headTitle($title = null, $setType = Zend_View_Helper_Placeholder_Container_Abstract::APPEND)
    {
        if (null !== $title && APPLICATION_ENV == 'development'){
            $title = strtolower($title);
        }
        return parent::headTitle($title, $setType);
    }
}

To use it, just add the helper path to your bootstrap. For example,you could do it in your config/application.ini with:

resources.view[] =
resources.view.helpers.App_View_Helper_ = "App/View/Helper"

The calls to $this->headTitle() in your view scripts or layout scripts remain unchanged.

As a side note, a great repository of Zend Framework snippets is at the aptly named site ZFSnippets.com.

Do you use any similar tricks or snippets in your development? Let me know via the comments.

Cheers!

4 comments so far »

  1. Jeremy Kendall said,

    Wrote on April 18, 2010 @ 10:43 pm

    Nice use of custom view helpers. I’m curious, why do you often have both prod and dev sites open at the same time?

    My favorite dev trick is one I took from Jaybill (http://jaybill.com/2007/10/01/the-most-useful-function-you-will-ever-use-in-the-zend-framework/).

    Those two functions have saved me more development time than I can measure.

  2. david said,

    Wrote on April 18, 2010 @ 11:39 pm

    @Jeremy: The shameful truth is that sometimes there is a bug in production and I find myself desperately hacking away on my dev side to identify and fix the problem. ;-( Other times, I will simply not have closed the production browser while I am working on the dev copy. So, maybe it’s not as often as I suggested.

    There is one sticky point with the view helper I eventually ran into. If the layout uses headTitle()->append(), then the appended text does not get lower-cased. Not critical, since it is the *beginning* of the title that helps me distinguish the browser window. But it would be nice to have the view helper apply lower-case to the appends, as well. Sadly, I was unable to make it work. Suggestions welcome.

  3. Joe Devon said,

    Wrote on June 9, 2010 @ 1:43 am

    @David

    Nice thought.

    I always try to change up background colors or skins in different environments. A simple example if you have a CMS pointing to a dev database vs. a prod database…you don’t want to make a test entry on prod by mistake.

  4. david said,

    Wrote on June 9, 2010 @ 9:55 am

    @Joe: That’s a good idea – some other visible difference in the skin or background based on environment. I’ll probably try that. Thanks!

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: