Zend Framework: View Helper to Render any HTML tag

Sometimes you might want to render a HTML tag with a lot of dynamic attributes in a view, of you need to create an HTML Element somewhere else were it is unlikely to use HTML with inline <?php tags.

I created a view helper that just takes an array of the attributes and returns the tag. The interesting part is thing is, that this functionality is already hidden in the Zend Framework within Zend_View_Helper_HtmlElement.

<?php

/*
 * This helper allows to render any HTML element with attribs from an array.
 * 
 * @author A. M.
 * @copyright Public Domain
 */
class Zend_View_Helper_AnyHtmlElement extends Zend_View_Helper_HtmlElement {

	/**
	 * @param tag The HTML tag, for example img 
	 * @param attribs HTML Attributes
	 * 
	 */
    public function anyHtmlElement($tag, $value = null, array $attribs = null)
    {
    	if ($value != null) throw new Zend_Exception('not implemented yet, we can make only <XXXXXX /> tags right now.');

    	$xhtml = '<' . $tag . ' ';
    	$xhtml .= $this->_htmlAttribs($attribs);
    	$xhtml .= $this->getClosingBracket();
        return $xhtml;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *