PageRenderTime 76ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Templates/Filters/TemplateHelpers.php

https://github.com/DocX/nette
PHP | 253 lines | 99 code | 51 blank | 103 comment | 15 complexity | 3590923b39debdbf97acde77995b18a4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Templates
  17. */
  18. /*namespace Nette\Templates;*/
  19. /**
  20. * Standard template run-time helpers shipped with Nette Framework.
  21. *
  22. * @author David Grudl
  23. * @copyright Copyright (c) 2004, 2009 David Grudl
  24. * @package Nette\Templates
  25. */
  26. final class TemplateHelpers
  27. {
  28. /**
  29. * Static class - cannot be instantiated.
  30. */
  31. final public function __construct()
  32. {
  33. throw new /*\*/LogicException("Cannot instantiate static class " . get_class($this));
  34. }
  35. /**
  36. * Try to load the requested helper.
  37. * @param string helper name
  38. * @return callback
  39. */
  40. public static function loader($helper)
  41. {
  42. $callback = 'Nette\Templates\TemplateHelpers::' . $helper;
  43. /**/fixCallback($callback);/**/
  44. if (is_callable($callback)) {
  45. return $callback;
  46. }
  47. $callback = 'Nette\String::' . $helper;
  48. /**/fixCallback($callback);/**/
  49. if (is_callable($callback)) {
  50. return $callback;
  51. }
  52. }
  53. /**
  54. * Escapes string for use inside HTML template.
  55. * @param mixed UTF-8 encoding or 8-bit
  56. * @return string
  57. */
  58. public static function escapeHtml($s)
  59. {
  60. if (is_object($s) && ($s instanceof ITemplate || $s instanceof /*Nette\Web\*/Html || $s instanceof /*Nette\Forms\*/Form)) {
  61. return $s->__toString(TRUE);
  62. }
  63. return htmlSpecialChars($s, ENT_QUOTES);
  64. }
  65. /**
  66. * Escapes string for use inside HTML comments.
  67. * @param mixed UTF-8 encoding or 8-bit
  68. * @return string
  69. */
  70. public static function escapeHtmlComment($s)
  71. {
  72. // -- has special meaning in different browsers
  73. return str_replace('--', '--><!--', $s); // HTML tags have no meaning inside comments
  74. }
  75. /**
  76. * Escapes string for use inside XML 1.0 template.
  77. * @param string UTF-8 encoding or 8-bit
  78. * @return string
  79. */
  80. public static function escapeXML($s)
  81. {
  82. // XML 1.0: \x09 \x0A \x0D and C1 allowed directly, C0 forbidden
  83. // XML 1.1: \x00 forbidden directly and as a character reference, \x09 \x0A \x0D \x85 allowed directly, C0, C1 and \x7F allowed as character references
  84. return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
  85. }
  86. /**
  87. * Escapes string for use inside CSS template.
  88. * @param string UTF-8 encoding or 8-bit
  89. * @return string
  90. */
  91. public static function escapeCss($s)
  92. {
  93. // http://www.w3.org/TR/2006/WD-CSS21-20060411/syndata.html#q6
  94. return addcslashes($s, "\x00..\x2C./:;<=>?@[\\]^`{|}~");
  95. }
  96. /**
  97. * Escapes string for use inside HTML style attribute.
  98. * @param string UTF-8 encoding or 8-bit
  99. * @return string
  100. */
  101. public static function escapeHtmlCss($s)
  102. {
  103. return htmlSpecialChars(self::escapeCss($s), ENT_QUOTES);
  104. }
  105. /**
  106. * Escapes string for use inside JavaScript template.
  107. * @param mixed UTF-8 encoding
  108. * @return string
  109. */
  110. public static function escapeJs($s)
  111. {
  112. if (is_object($s) && ($s instanceof ITemplate || $s instanceof /*Nette\Web\*/Html || $s instanceof /*Nette\Forms\*/Form)) {
  113. $s = $s->__toString(TRUE);
  114. }
  115. return str_replace(']]>', ']]\x3E', json_encode($s));
  116. }
  117. /**
  118. * Escapes string for use inside HTML JavaScript attribute.
  119. * @param mixed UTF-8 encoding
  120. * @return string
  121. */
  122. public static function escapeHtmlJs($s)
  123. {
  124. return htmlSpecialChars(self::escapeJs($s), ENT_QUOTES);
  125. }
  126. /**
  127. * Replaces all repeated white spaces with a single space.
  128. * @param string UTF-8 encoding or 8-bit
  129. * @return string
  130. */
  131. public static function strip($s)
  132. {
  133. $s = preg_replace_callback('#<(textarea|pre|script).*?</\\1#si', array(__CLASS__, 'indentCb'), $s);
  134. $s = trim(preg_replace('#[ \t\r\n]+#', ' ', $s));
  135. return strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
  136. }
  137. /**
  138. * Indents the HTML content from the left.
  139. * @param string UTF-8 encoding or 8-bit
  140. * @param int
  141. * @param string
  142. * @return string
  143. */
  144. public static function indent($s, $level = 1, $chars = "\t")
  145. {
  146. if ($level >= 1) {
  147. $s = preg_replace_callback('#<(textarea|pre).*?</\\1#si', array(__CLASS__, 'indentCb'), $s);
  148. $s = /*Nette\*/String::indent($s, $level, $chars);
  149. $s = strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
  150. }
  151. return $s;
  152. }
  153. /**
  154. * Callback for self::indent
  155. */
  156. private static function indentCb($m)
  157. {
  158. return strtr($m[0], " \t\r\n", "\x1F\x1E\x1D\x1A");
  159. }
  160. /**
  161. * Date/time formatting.
  162. * @param string|int|DateTime
  163. * @param string
  164. * @return string
  165. */
  166. public static function date($time, $format = "%x")
  167. {
  168. if ($time == NULL) { // intentionally ==
  169. return NULL;
  170. } elseif (!($time instanceof /*\*/DateTime)) {
  171. $time = new /*\*/DateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
  172. }
  173. return strpos($format, '%') === FALSE
  174. ? $time->format($format) // formats using date()
  175. : strftime($format, $time->format('U')); // formats according to locales
  176. }
  177. /**
  178. * Converts to human readable file size.
  179. * @param int
  180. * @param int
  181. * @return string
  182. */
  183. public static function bytes($bytes, $precision = 2)
  184. {
  185. $bytes = round($bytes);
  186. $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
  187. foreach ($units as $unit) {
  188. if (abs($bytes) < 1024 || $unit === end($units)) break;
  189. $bytes = $bytes / 1024;
  190. }
  191. return round($bytes, $precision) . ' ' . $unit;
  192. }
  193. /**
  194. * /dev/null.
  195. * @param mixed
  196. * @return string
  197. */
  198. public static function null($value)
  199. {
  200. return '';
  201. }
  202. }