Skip to content
chris.l.yates edited this page May 21, 2010 · 1 revision
  1. summary Haml Helper Methods.

Introduction

PHamlP provides some helper methods to make it easier to do various tasks.

The HamlHelpers 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.

The 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.

Helpers are used within a template like a function and act on a block of Haml code (html_attr() is the exception). e.g.

produces

Methods

  • append($string)*
Appends string to the block
  • escape_once()*
Escapes HTML entities in text, but without escaping an ampersand that is already part of an escaped entity.
  • html_attrs($language = 'en-us', $lang = true)*
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:

produces

produces

produces



Although handled in HamlParser, the notes below are here for completeness.

Other attributes are defined as normal. e.g.

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.

produces (assuming FW::app()->language returns 'en-gb')

  • html_escape()*
Returns a copy of text with ampersands, angle brackets and quotes escaped into HTML entities.
  • list_of($block, $items, $key, $value = null)*
Iterates an array and using the block to generate a `
  • ` element for each array element. Examples:

    Produces:

    And

    Produces:

    • preceed($string)*
    Alias for prepend.
    • prepend($string)*
    Returns the block with string prepended.
    • preserve()*
    Converts newlines in the block to HTML entities.
    • succeed($string)*
    Alias for append.
    • surround($front, $back=null)*
    Surrounds a block of Haml code with strings. If $back is not given it defaults to $front.
  • Clone this wiki locally