Zend Framework Body Tag View Helper

By , Saturday 21st August 2010 11:13 pm
Photo from  http://www.flickr.com/photos/daniello/

Photo from http://www.flickr.com/photos/daniello/

Summary

Here I discuss the creation of a view helper for modifying HTML tags, and more specifically body tags. The created view helper allows functionality similar to the head*/inlineScript view helpers already in the standard Zend Framework view helpers, but allows the programmatic modification of tag attributes. Definitely check out the demo page and the code on github.

Motivation

The standard Zend Framework view helpers are a great set of tools for streamlining mundane view tasks and allowing for the modification/addition of scripts and header blocks (generally held in the layout) from within the view without applying ugly hacks (i.e. the head*/inlineScript view helpers).

Upon occasion I have found need to make modifications to the <body> tag, for example adding an onload, class, or style attribute etc. I also required to be able to perform this from within other view helpers. Take this following contrived example…

On website X, certain pages include standard dojo forms. These dojo forms are held within view helpers for convenience. Generally it has been decided not to include the dojo CSS classes in the body tag and only add them when necessary. There maybe several view helpers on the page that need to add their own attributes to the body tag. (I said it was contrived)

The code is available in my GIT repository @ github and the demo page.

How it works

When creating the body tag view helper I soon realised that most of the code was not specific to what I was developing so I pulled out the common functionality into an abstract class.  By concreting the  abstract class it is possible to modify any tag in the same way as the body tag discussed here making the code much more versitile.

Underneath the hood (so to speak) the code is based on that of the head* and the inlineScript view helpers. It utilises Zend_View_Helper_Placeholder_Container_Standalone to store the attributes and their values until they are required. The container itself uses Zend_Registry to store values under the key defined in the class (if you look at the unit tests I clear the registry down after each test).

The code would fit in well with the “Naked Zend_Layout and Zend_View” code I wrote about a couple of weeks ago or as part of the standard Zend Framework MVC install.

Usage

The class should be very simple to use. Firstly two entries are required within your layout script as follows:

<?php echo $this->bodyTag() ?>
<?php echo $this->bodyTag()->toString(Pro_View_Helper_AbstractTag::CLOSE) ?>

Note: You’ll need to change the class name should you move the view helper to your own library. Obviously the constant is available through the implementation (bodyTag) as well as through the abstract class.

An example of making changes to your body tag using the view helper to echo <body class=”nihilo myclass” style=”text-align-left; width: 80em”> is:

<?php
$this->bodyTag('class', 'nihilo')
    ->bodyTag('style', 'text-align: left')
    ->bodyTag('style', 'width: 80em', false, ';')
    ->bodyTag('class', 'myclass')
?>

This can be done throughout the view file, in several view helpers, and in the layout file (provided its before the body tag is written out). Attributes can be removed/overwritten whilst values can be added and appended (separator can be passed). If the same attribute value is passed several times only a single copy is written with the attribute.

For a full demo either fork the github repository and run the demo.php file in your own browser, or alternatively visit this page to see it in action. Usage of the demo.php script assumes you have the Zend Autoloader (or similar) already setup.

Creating your own Tag helper

To implement your own view helper simply extend the abstract tag class and overwrite the following class properties:

  • $_regKey: A unique key for the tag (used for Zend_Registry)
  • $_validAttributes: An array of valid attribute names, should be lowercase to comply with W3C
  • $_selfClosing: Boolean value to specify if tag is self closing or not, e.g. <br/>
  • $_tagName: Name of the tag (i.e. body in this example)

So, finally the code for the bodyTag view helper itself, its very compact:

/**
 * View helper for the body tag
 *
 * @author     Lloyd Watkin
 * @since      21/08/2010
 * @package    Pro
 * @subpackage ViewHelper
 */
class Pro_View_Helper_BodyTag
    extends Pro_View_Helper_AbstractTag
{
    /**
     * Registry key for placeholder
     *
     * @var string
     */
    protected $_regKey = 'Pro_View_Helper_BodyTag';

    /**
     * Which attributes are valid
     *
     * Currently only STF attributes supported
     * (S = STRICT, T = TRANSITIONAL, F = FRAMESET)
     *
     * @see http://www.w3schools.com/tags/tag_body.asp
     * @var array
     */
    protected $_validAttributes = array(
        /* Standard Attributes */
        'class', 'dir', 'id', 'lang', 'style', 'title', 'xml:lang',
        /* Event Attributes */
        'onclick', 'ondblclick', 'onload', 'onmousedown', 'onmousemove',
        'onmouseout', 'onmouseover', 'onmouseup', 'onkeydown', 'onkeypress',
        'onkeyup', 'onunload',
    );

    /**
     * Self closing tag?
     *
     * @var boolean
     */
    protected $_selfClosing = false;    

    /**
     * Tag name
     *
     * @var string
     */
    protected $_tagName = 'body';
}

If you take a look at the repository there’s also a group of unit tests that fully cover the functionality. The unit tests are written in PHPUnit and are in the BodyTagTest.php file.

Finally…

Here I’ve shown you my implementation of an abstract tag abstract view helper (eek) and its body tag implementation. Whilst the javaScript functionality can be implemented in other ways (other than hard coding at output time) other things can’t (without javaScript that is) without changing other parts of the application. I hope you find the classes helpful in your application…

Liked this post? Follow this blog to get more. 

3 Responses to “Zend Framework Body Tag View Helper”

  1. My automagic linker has caused on issue in the code for XML so please ignore that issue :)

  2. […] the original here: Evilprofessor.co.uk » Zend Framework Body Tag View Helper No […]

  3. […] more from the original source: Evilprofessor.co.uk » Zend Framework Body Tag View Helper Share and […]

Leave a Reply

You must be logged in to post a comment.

Panorama Theme by Themocracy

3 visitors online now
2 guests, 1 bots, 0 members
Max visitors today: 6 at 06:58 am UTC
This month: 11 at 16-04-2017 11:37 pm UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC