Source for file TemplateFilters.php

Documentation is available at TemplateFilters.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Templates
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: /**
  24. 24:  * Standard template filters shipped with Nette Framework.
  25. 25:  *
  26. 26:  * @author     David Grudl
  27. 27:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  28. 28:  * @package    Nette\Templates
  29. 29:  */
  30. 30: final class TemplateFilters
  31. 31: {
  32. 32:  
  33. 33:     /**
  34. 34:      * Static class - cannot be instantiated.
  35. 35:      */
  36. 36:     final public function __construct()
  37. 37:     {
  38. 38:         throw new LogicException("Cannot instantiate static class " get_class($this));
  39. 39:     }
  40. 40:  
  41. 41:  
  42. 42:  
  43. 43:     /********************* Filter curlyBrackets ****************d*g**/
  44. 44:  
  45. 45:  
  46. 46:     /** @deprecated */
  47. 47:     public static $curlyXlatMask;
  48. 48:  
  49. 49:  
  50. 50:  
  51. 51:     /** @deprecated */
  52. 52:     public static function curlyBrackets($s)
  53. 53:     {
  54. 54:         trigger_error(__METHOD__ . '() is deprecated; use $template->registerFilter(\'CurlyBracketsFilter::invoke\') instead'E_USER_WARNING);
  55. 55:         return CurlyBracketsFilter::invoke($s);
  56. 56:     }
  57. 57:  
  58. 58:  
  59. 59:  
  60. 60:     /********************* Filter fragments ****************d*g**/
  61. 61:  
  62. 62:  
  63. 63:  
  64. 64:     /** @deprecated */
  65. 65:     public static function fragments($s)
  66. 66:     {
  67. 67:         trigger_error(__METHOD__ . '() is deprecated; use {block}...{/block} instead'E_USER_WARNING);
  68. 68:     }
  69. 69:  
  70. 70:  
  71. 71:  
  72. 72:     /********************* Filter removePhp ****************d*g**/
  73. 73:  
  74. 74:  
  75. 75:  
  76. 76:     /**
  77. 77:      * Filters out PHP code.
  78. 78:      *
  79. 79:      * @param  string 
  80. 80:      * @return string 
  81. 81:      */
  82. 82:     public static function removePhp($s)
  83. 83:     {
  84. 84:         return preg_replace('#\x01@php:p\d+@\x02#''<?php ?>'$s)// Template hides PHP code in these snippets
  85. 85:     }
  86. 86:  
  87. 87:  
  88. 88:  
  89. 89:     /********************* Filter autoConfig ****************d*g**/
  90. 90:  
  91. 91:  
  92. 92:  
  93. 93:     /**
  94. 94:      * Template with configuration (experimental).
  95. 95:      *    <?nette filter="CurlyBracketsFilter::invoke"?>
  96. 96:      *
  97. 97:      * @param  string 
  98. 98:      * @return string 
  99. 99:      */
  100. 100:     public static function autoConfig($s)
  101. 101:     {
  102. 102:         throw new NotImplementedException;
  103. 103:         preg_match_all('#<\\?nette(.*)\\?>#sU'$s$matchesPREG_SET_ORDER);
  104. 104:         foreach ($matches as $m{
  105. 105:         }
  106. 106:         return preg_replace('#<\\?nette(.*)\\?>#sU'''$s);
  107. 107:     }
  108. 108:  
  109. 109:  
  110. 110:  
  111. 111:     /********************* Filter relativeLinks ****************d*g**/
  112. 112:  
  113. 113:  
  114. 114:  
  115. 115:     /**
  116. 116:      * Filter relativeLinks: prepends root to relative links.
  117. 117:      * @param  string 
  118. 118:      * @return string 
  119. 119:      */
  120. 120:     public static function relativeLinks($s)
  121. 121:     {
  122. 122:         return preg_replace(
  123. 123:             '#(src|href|action)\s*=\s*(["\'])(?![a-z]+:|[\x01/\\#])#'// \x01 is PHP snippet
  124. 124:             '$1=$2<?php echo \\$baseUri ?>',
  125. 125:             $s
  126. 126:         );
  127. 127:     }
  128. 128:  
  129. 129:  
  130. 130:  
  131. 131:     /********************* Filter netteLinks ****************d*g**/
  132. 132:  
  133. 133:  
  134. 134:  
  135. 135:     /**
  136. 136:      * Filter netteLinks: translates links "nette:...".
  137. 137:      *   nette:destination?arg
  138. 138:      * @param  string 
  139. 139:      * @return string 
  140. 140:      */
  141. 141:     public static function netteLinks($s)
  142. 142:     {
  143. 143:         return preg_replace_callback(
  144. 144:             '#(src|href|action|on[a-z]+)\s*=\s*["\'](nette:.*?)([\#"\'])#',
  145. 145:             array(__CLASS__'tnlCb'),
  146. 146:             $s)
  147. 147:         ;
  148. 148:     }
  149. 149:  
  150. 150:  
  151. 151:  
  152. 152:     /**
  153. 153:      * Callback for self::netteLinks.
  154. 154:      * Parses a "nette" URI (scheme is 'nette') and converts to real URI
  155. 155:      */
  156. 156:     private static function tnlCb($m)
  157. 157:     {
  158. 158:         list($attr$uri$fragment$m;
  159. 159:  
  160. 160:         $parts parse_url($uri);
  161. 161:         if (isset($parts['scheme']&& $parts['scheme'=== 'nette'{
  162. 162:             return $attr '="<?php echo $template->escape($control->'
  163. 163:                 . (strncmp($attr'on'2'link' 'ajaxLink')
  164. 164:                 . '(\''
  165. 165:                 . (isset($parts['path']$parts['path''this!')
  166. 166:                 . (isset($parts['query']'?' $parts['query''')
  167. 167:                 . '\'))?>'
  168. 168:                 . $fragment;
  169. 169:         else {
  170. 170:             return $m[0];
  171. 171:         }
  172. 172:     }
  173. 173:  
  174. 174:  
  175. 175:  
  176. 176:     /********************* Filter texyElements ****************d*g**/
  177. 177:  
  178. 178:  
  179. 179:  
  180. 180:     /** @var Texy */
  181. 181:     public static $texy;
  182. 182:  
  183. 183:  
  184. 184:  
  185. 185:     /**
  186. 186:      * Process <texy>...</texy> elements.
  187. 187:      * @param  string 
  188. 188:      * @return string 
  189. 189:      */
  190. 190:     public static function texyElements($s)
  191. 191:     {
  192. 192:         return preg_replace_callback(
  193. 193:             '#<texy([^>]*)>(.*?)</texy>#s',
  194. 194:             array(__CLASS__'texyCb'),
  195. 195:             $s
  196. 196:         );
  197. 197:     }
  198. 198:  
  199. 199:  
  200. 200:  
  201. 201:     /**
  202. 202:      * Callback for self::texyBlocks.
  203. 203:      */
  204. 204:     private static function texyCb($m)
  205. 205:     {
  206. 206:         list($mAttrs$mContent$m;
  207. 207:  
  208. 208:         // parse attributes
  209. 209:         $attrs array();
  210. 210:         if ($mAttrs{
  211. 211:             preg_match_all(
  212. 212:                 '#([a-z0-9:-]+)\s*(?:=\s*(\'[^\']*\'|"[^"]*"|[^\'"\s]+))?()#isu',
  213. 213:                 $mAttrs,
  214. 214:                 $arr,
  215. 215:                 PREG_SET_ORDER
  216. 216:             );
  217. 217:  
  218. 218:             foreach ($arr as $m{
  219. 219:                 $key strtolower($m[1]);
  220. 220:                 $val $m[2];
  221. 221:                 if ($val == NULL$attrs[$keyTRUE;
  222. 222:                 elseif ($val{0=== '\'' || $val{0=== '"'$attrs[$keyhtml_entity_decode(substr($val1-1)ENT_QUOTES'UTF-8');
  223. 223:                 else $attrs[$keyhtml_entity_decode($valENT_QUOTES'UTF-8');
  224. 224:             }
  225. 225:         }
  226. 226:  
  227. 227:         return self::$texy->process($m[2]);
  228. 228:     }
  229. 229:  
  230. 231:  
  231. 232:  
  232. 233: // back compatiblity:
  233. 234: TemplateFilters::$curlyXlatMask CurlyBracketsFilter::$macros;