HamlHelpers.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /* SVN FILE: $Id: HamlHelpers.php 92 2010-05-20 17:42:59Z chris.l.yates $ */
  3. /**
  4. * HamlHelpers class file.
  5. *
  6. * @author Chris Yates <chris.l.yates@gmail.com>
  7. * @copyright Copyright (c) 2010 PBM Web Development
  8. * @license http://phamlp.googlecode.com/files/license.txt
  9. * @package PHamlP
  10. * @subpackage Haml
  11. */
  12. /**
  13. * HamlHelpers class.
  14. * Contains methods to make it easier to do various tasks.
  15. *
  16. * The class can be extended to provide user defined helper methods. The
  17. * signature for user defined helper methods is ($block, $other, $arguments);
  18. * $block is the string generated by the Haml block being operated on.
  19. *
  20. * Tthe path to the extended class is provided to HamlParser in the config
  21. * array; class name == file name.
  22. *
  23. * HamlHelpers and any extended class are automatically included in the context
  24. * that a Haml template is parsed in, so all the methods are at your disposal
  25. * from within the template.
  26. *
  27. * @package PHamlP
  28. * @subpackage Haml
  29. */
  30. class HamlHelpers {
  31. const XMLNS = 'http://www.w3.org/1999/xhtml';
  32. /**
  33. * Returns the block with string appended.
  34. * @see succeed
  35. * @param string Haml block
  36. * @param string string to append
  37. * @return string the block with string appended.
  38. */
  39. public static function append($block, $string) {
  40. return $block.$string;
  41. }
  42. /**
  43. * Escapes HTML entities in text, but without escaping an ampersand that is
  44. * already part of an escaped entity.
  45. * @param string Haml block
  46. * @return string the block with HTML entities escaped.
  47. */
  48. public static function escape_once($block) {
  49. return htmlentities(html_entity_decode($block));
  50. }
  51. /**
  52. * Returns an array containing default assignments for the xmlns, lang, and
  53. * xml:lang attributes of the html element.
  54. * This helper method is for use in the html element only.
  55. *
  56. * Examples:<br/>
  57. * %html(html_attrs())<br/>
  58. * produces<br/>
  59. * <html lang="en-us" xml:lang="en-us" xmlns="http://www.w3.org/1999/xhtml">
  60. *
  61. * %html(html_attrs('en-gb'))<br/>
  62. * produces<br/>
  63. * <html lang="en-gb" xml:lang="en-gb" xmlns="http://www.w3.org/1999/xhtml">
  64. *
  65. * %html(html_attrs('en-gb', false))<br/>
  66. * produces<br/>
  67. * <html xml:lang="en-gb" xmlns="http://www.w3.org/1999/xhtml">
  68. *
  69. * Although handled in HamlParser, the notes below are here for completeness.<br/>
  70. * Other attributes are defined as normal. e.g.<br/>
  71. * %html(xmlns:me="http://www.example.com/me" html_attrs('en-gb', false))<br/>
  72. * produces<br/>
  73. * <html xml:lang="en-gb" xmlns="http://www.w3.org/1999/xhtml" xmlns:me="http://www.example.com/me">
  74. *
  75. * PHamlP also allows for the language to be defined using PHP code that can
  76. * be eval'd; the code must end with a semi-colon (;). e.g.<br/>
  77. * %html(html_attrs("FW::app()->language);", false))<br/>
  78. * produces (assuming FW::app()->language returns 'en-gb')<br/>
  79. * <html xml:lang="en-gb" xmlns="http://www.w3.org/1999/xhtml">
  80. *
  81. * @param string document language. Default = en-us
  82. * @param boolean whether the html element has the lang attribute. Default: true
  83. * Should be set false for XHTML 1.1 or greater documents
  84. * @return string the block with string appended.
  85. */
  86. public static function html_attrs($language = 'en-us', $lang = true) {
  87. return ($lang ?
  88. array('xmlns'=>self::XMLNS, 'xml:lang'=>$language, 'lang'=>$language) :
  89. array('xmlns'=>self::XMLNS, 'xml:lang'=>$language));
  90. }
  91. /**
  92. * Returns a copy of text with ampersands, angle brackets and quotes escaped
  93. * into HTML entities.
  94. * @param string Haml block
  95. * @return string the block with HTML entities escaped.
  96. */
  97. public static function html_escape($block) {
  98. return htmlspecialchars($block);
  99. }
  100. /**
  101. * Iterates an array and using the block to generate a <li> element for each
  102. * array element.
  103. * Examples:<br/>
  104. * = list_of(array('red', 'orange', ...., 'violet'), 'colour')<br/>
  105. * = colour<br/>
  106. * Produces:<br/>
  107. * <li>red</li><br/>
  108. * <li>orange</li><br/>
  109. * |<br/>
  110. * |<br/>
  111. * <li>violet></li><br/>
  112. *
  113. * = list_of(array('Fly Fishing' => 'JR Hartley', 'Lord of the Rings' => 'JRR Tolkien'), 'title', 'author')<br/>
  114. * %h3= title<br/>
  115. * %p= author<br/>
  116. * Produces:<br/>
  117. * <li><br/>
  118. * <h3>Fly Fishing</h3><br/>
  119. * <p>JR Hartley</p><br/>
  120. * </li><br/>
  121. * <li><br/>
  122. * <h3>Lord of the Rings</h3><br/>
  123. * <p>JRR Tolkien</p><br/>
  124. * </li><br/>
  125. *
  126. * @param string Haml block
  127. * @param array items
  128. * @param string string in block to replace with item key or item value
  129. * @param string string in block to replace with item value
  130. * @return string list items.
  131. */
  132. public static function list_of($block, $items, $key, $value = null) {
  133. $output = '';
  134. foreach ($items as $_key=>$_value) {
  135. $output .= '<li>' . strtr($block, (empty($value) ? array($key=>$_value) :
  136. array($key=>$_key, $value=>$_value))) . '</li>';
  137. } // foreach
  138. return $output;
  139. }
  140. /**
  141. * Alias for prepend.
  142. * @see prepend
  143. * @param string Haml block
  144. * @param string string to prepend
  145. * @return string the block with string prepended
  146. */
  147. public static function preceed($block, $string) {
  148. return self::prepend($block, $string);
  149. }
  150. /**
  151. * Returns the block with string prepended.
  152. * @param string Haml block
  153. * @param string string to prepend
  154. * @return string the block with string prepended
  155. */
  156. public static function prepend($block, $string) {
  157. return $string.$block;
  158. }
  159. /**
  160. * Converts newlines in the block to HTML entities.
  161. * @param string Haml block
  162. * @return string the block with newlines converted to HTML entities
  163. */
  164. public static function preserve($block) {
  165. return str_replace("\n", '&#x0d;&#x0a;', $block);
  166. }
  167. /**
  168. * Alias for append.
  169. * @see append
  170. * @param string Haml block
  171. * @param string string to apppend
  172. * @return string the block with string apppended
  173. */
  174. public static function succeed($block, $string) {
  175. return self::append($block, $string);
  176. }
  177. /**
  178. * Surrounds a block of Haml code with strings.
  179. * If $back is not given it defaults to $front.
  180. * @param string Haml block
  181. * @param string string to prepend
  182. * @param string string to apppend
  183. * @return string the block surrounded by the strings
  184. */
  185. public static function surround($block, $front, $back=null) {
  186. return $front.$block.(is_null($back) ? $front : $back);
  187. }
  188. }