* @copyright Copyright (c) 2010 PBM Web Development
* @license http://phamlp.googlecode.com/files/license.txt
* @package PHamlP
* @subpackage Haml
*/
/**
* HamlHelpers class.
* Contains methods to make it easier to do various tasks.
*
* The class can be extended to provide user defined helper methods. The
* signature for user defined helper methods is ($block, $other, $arguments);
* $block is the string generated by the Haml block being operated on.
*
* Tthe path to the extended class is provided to HamlParser in the config
* array; class name == file name.
*
* HamlHelpers and any extended class are automatically included in the context
* that a Haml template is parsed in, so all the methods are at your disposal
* from within the template.
*
* @package PHamlP
* @subpackage Haml
*/
class HamlHelpers {
const XMLNS = 'http://www.w3.org/1999/xhtml';
/**
* Returns the block with string appended.
* @see succeed
* @param string Haml block
* @param string string to append
* @return string the block with string appended.
*/
public static function append($block, $string) {
return $block.$string;
}
/**
* Escapes HTML entities in text, but without escaping an ampersand that is
* already part of an escaped entity.
* @param string Haml block
* @return string the block with HTML entities escaped.
*/
public static function escape_once($block) {
return htmlentities(html_entity_decode($block));
}
/**
* Returns an array containing default assignments for the xmlns, lang, and
* xml:lang attributes of the html element.
* This helper method is for use in the html element only.
*
* Examples:
* %html(html_attrs())
* produces
*
*
* %html(html_attrs('en-gb'))
* produces
*
*
* %html(html_attrs('en-gb', false))
* produces
*
*
* Although handled in HamlParser, the notes below are here for completeness.
* Other attributes are defined as normal. e.g.
* %html(xmlns:me="http://www.example.com/me" html_attrs('en-gb', false))
* produces
*
*
* PHamlP also allows for the language to be defined using PHP code that can
* be eval'd; the code must end with a semi-colon (;). e.g.
* %html(html_attrs("FW::app()->language);", false))
* produces (assuming FW::app()->language returns 'en-gb')
*
*
* @param string document language. Default = en-us
* @param boolean whether the html element has the lang attribute. Default: true
* Should be set false for XHTML 1.1 or greater documents
* @return string the block with string appended.
*/
public static function html_attrs($language = 'en-us', $lang = true) {
return ($lang ?
array('xmlns'=>self::XMLNS, 'xml:lang'=>$language, 'lang'=>$language) :
array('xmlns'=>self::XMLNS, 'xml:lang'=>$language));
}
/**
* Returns a copy of text with ampersands, angle brackets and quotes escaped
* into HTML entities.
* @param string Haml block
* @return string the block with HTML entities escaped.
*/
public static function html_escape($block) {
return htmlspecialchars($block);
}
/**
* Iterates an array and using the block to generate a
JR Hartley
JRR Tolkien