Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Helper functions

svallory edited this page Jul 5, 2012 · 1 revision

Helper functions

HamlPHP has some helper functions that can make your life a little easier. As of now, there are four helpers, two used internally (quote() and atts()), but available to you, and two just for you (id_for() and class_for()).

quote($str)

@param $str
@return mixed The quoted string or false

Returns a double or single quoted string. whatever works without changing the content of the string.

Example:

quote("McDonald's");              // -> "\\"McDonad's\\""
quote("You \\"win\\", looser");   // -> "'You \\"win\\", looser'" 

If it can't be done. It will return false.

atts($atts, $echo=true)

@param array $atts
@param bool $echo [optional] Wheter to echo the result or not (default: true)
@return string The list of attributes

Prints a list of attributes specified by an array of (att_name => att_value). If the value of the attribute id is an array, it will call join the elements using '_'. If the value of class attribute is an array, it will join the values with a white space between them.

id_for($obj, $prefix = '')

@param object $obj
@param string $prefix [Optional]

Returns an id generated by joining the class of $obj and the id of $obj, joined by an underscore (_), and prefixing it with $prefix.

To get the class, this method will call the class_for() helper.

To get the id, this method first check if the $id property is set, if not, it checks for a getId() method. If none of them is found, it will use an internal, class specific, object counter.

class_for($obj, $prefix = '')

@param object $obj
@param string $prefix [Optional]

Returns the class name of an object to be used in the class attribute.

This behaviour can be overloaded by implementing a haml_obj_ref (or hamlObjRef) method on the class, which MUST return a string. If such method isn't present, it will use php's get_class() function.

If prefix is not empty, both, the object's class and the prefix, will be returned separated by one space.