Source for file CurlyBracketsMacros.php

Documentation is available at CurlyBracketsMacros.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 http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Templates
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../../Object.php';
  23. 23:  
  24. 24:  
  25. 25:  
  26. 26: /**
  27. 27:  * Default macros for filter CurlyBracketsFilter.
  28. 28:  *
  29. 29:  * - {$variable} with escaping
  30. 30:  * - {!$variable} without escaping
  31. 31:  * - {*comment*} will be removed
  32. 32:  * - {=expression} echo with escaping
  33. 33:  * - {!=expression} echo without escaping
  34. 34:  * - {?expression} evaluate PHP statement
  35. 35:  * - {_expression} echo translation with escaping
  36. 36:  * - {!_expression} echo translation without escaping
  37. 37:  * - {link destination ...} control link
  38. 38:  * - {plink destination ...} presenter link
  39. 39:  * - {if ?} ... {elseif ?} ... {else} ... {/if}
  40. 40:  * - {ifset ?} ... {elseifset ?} ... {/if}
  41. 41:  * - {for ?} ... {/for}
  42. 42:  * - {foreach ?} ... {/foreach}
  43. 43:  * - {include ?}
  44. 44:  * - {cache ?} ... {/cache} cached block
  45. 45:  * - {snippet ?} ... {/snippet ?} control snippet
  46. 46:  * - {attr ?} HTML element attributes
  47. 47:  * - {block|texy} ... {/block} capture of filter block
  48. 48:  * - {contentType ...} HTTP Content-Type header
  49. 49:  * - {assign var => value} set template parameter
  50. 50:  * - {default var => value} set default template parameter
  51. 51:  * - {dump $var}
  52. 52:  * - {debugbreak}
  53. 53:  *
  54. 54:  * @author     David Grudl
  55. 55:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  56. 56:  * @package    Nette\Templates
  57. 57:  */
  58. 58: class CurlyBracketsMacros extends Object
  59. 59: {
  60. 60:     /** @var array */
  61. 61:     public static $defaultMacros array(
  62. 62:         'block' => '<?php %:macroBlock% ?>',
  63. 63:         '/block' => '<?php %:macroBlockEnd% ?>',
  64. 64:  
  65. 65:         'snippet' => '<?php } if ($_cb->foo = SnippetHelper::create($control%:macroSnippet%)) { $_cb->snippets[] = $_cb->foo; ?>',
  66. 66:         '/snippet' => '<?php array_pop($_cb->snippets)->finish(); } if (SnippetHelper::$outputAllowed) { ?>',
  67. 67:  
  68. 68:         'cache' => '<?php if ($_cb->foo = CachingHelper::create($_cb->key = md5(__FILE__) . __LINE__, $template->getFile(), array(%%))) { $_cb->caches[] = $_cb->foo; ?>',
  69. 69:         '/cache' => '<?php array_pop($_cb->caches)->save(); } if (!empty($_cb->caches)) end($_cb->caches)->addItem($_cb->key); ?>',
  70. 70:  
  71. 71:         'if' => '<?php if (%%): ?>',
  72. 72:         'elseif' => '<?php ; elseif (%%): ?>',
  73. 73:         'else' => '<?php ; else: ?>',
  74. 74:         '/if' => '<?php endif ?>',
  75. 75:         'ifset' => '<?php if (isset(%%)): ?>',
  76. 76:         'elseifset' => '<?php ; elseif (isset(%%)): ?>',
  77. 77:         'foreach' => '<?php foreach (%:macroForeach%): ?>',
  78. 78:         '/foreach' => '<?php endforeach; array_pop($_cb->its); $iterator = end($_cb->its) ?>',
  79. 79:         'for' => '<?php for (%%): ?>',
  80. 80:         '/for' => '<?php endfor ?>',
  81. 81:         'while' => '<?php while (%%): ?>',
  82. 82:         '/while' => '<?php endwhile ?>',
  83. 83:         'continueIf' => '<?php if (%%) continue ?>',
  84. 84:         'breakIf' => '<?php if (%%) break ?>',
  85. 85:  
  86. 86:         'include' => '<?php %:macroInclude% ?>',
  87. 87:         'extends' => '<?php %:macroExtends% ?>',
  88. 88:  
  89. 89:         'plink' => '<?php echo %:macroEscape%(%:macroPlink%) ?>',
  90. 90:         'link' => '<?php echo %:macroEscape%(%:macroLink%) ?>',
  91. 91:         'ifCurrent' => '<?php %:macroIfCurrent%; if ($presenter->getLastCreatedRequestFlag("current")): ?>',
  92. 92:         'widget' => '<?php %:macroWidget% ?>',
  93. 93:         'control' => '<?php %:macroWidget% ?>',
  94. 94:  
  95. 95:         'attr' => '<?php echo Html::el(NULL)->%:macroAttr%attributes() ?>',
  96. 96:         'contentType' => '<?php %:macroContentType% ?>',
  97. 97:         'assign' => '<?php %:macroAssign% ?>'// deprecated?
  98. 98:         'default' => '<?php %:macroDefault% ?>',
  99. 99:         'dump' => '<?php Debug::consoleDump(%:macroDump%, "Template " . str_replace(Environment::getVariable("templatesDir"), "\xE2\x80\xA6", $template->getFile())) ?>',
  100. 100:         'debugbreak' => '<?php if (function_exists("debugbreak")) debugbreak() ?>',
  101. 101:  
  102. 102:         '!_' => '<?php echo $template->translate(%:macroModifiers%) ?>',
  103. 103:         '!=' => '<?php echo %:macroModifiers% ?>',
  104. 104:         '_' => '<?php echo %:macroEscape%($template->translate(%:macroModifiers%)) ?>',
  105. 105:         '=' => '<?php echo %:macroEscape%(%:macroModifiers%) ?>',
  106. 106:         '!$' => '<?php echo %:macroVar% ?>',
  107. 107:         '!' => '<?php echo %:macroVar% ?>'// deprecated
  108. 108:         '$' => '<?php echo %:macroEscape%(%:macroVar%) ?>',
  109. 109:         '?' => '<?php %:macroModifiers% ?>'// deprecated?
  110. 110:     );
  111. 111:  
  112. 112:     /** @var array */
  113. 113:     public $macros;
  114. 114:  
  115. 115:     /** @var CurlyBracketsFilter */
  116. 116:     private $filter;
  117. 117:  
  118. 118:     /** @var array */
  119. 119:     private $current;
  120. 120:  
  121. 121:     /** @var array */
  122. 122:     private $blocks array();
  123. 123:  
  124. 124:     /** @var array */
  125. 125:     private $namedBlocks array();
  126. 126:  
  127. 127:     /** @var bool */
  128. 128:     private $extends;
  129. 129:  
  130. 130:     /** @var string */
  131. 131:     private $uniq;
  132. 132:  
  133. 133:  
  134. 134:  
  135. 135:     /**
  136. 136:      * Constructor.
  137. 137:      */
  138. 138:     public function __construct()
  139. 139:     {
  140. 140:         $this->macros = self::$defaultMacros;
  141. 141:     }
  142. 142:  
  143. 143:  
  144. 144:  
  145. 145:     /**
  146. 146:      * Initializes parsing.
  147. 147:      * @param  CurlyBracketsFilter 
  148. 148:      * @param  string 
  149. 149:      * @return void 
  150. 150:      */
  151. 151:     public function initialize($filter$s)
  152. 152:     {
  153. 153:         $this->filter $filter;
  154. 154:         $this->blocks array();
  155. 155:         $this->namedBlocks array();
  156. 156:         $this->extends NULL;
  157. 157:         $this->uniq substr(md5(uniqid())010);
  158. 158:  
  159. 159:         $filter->context CurlyBracketsFilter::CONTEXT_TEXT;
  160. 160:         $filter->escape 'TemplateHelpers::escapeHtml';
  161. 161:  
  162. 162:         // remove comments
  163. 163:         $s preg_replace('#\\{\\*.*?\\*\\}[\r\n]*#s'''$s);
  164. 164:  
  165. 165:         // snippets support (temporary solution)
  166. 166:         $s preg_replace(
  167. 167:             '#@(\\{[^}]+?\\})#s',
  168. 168:             '<?php } ?>$1<?php if (SnippetHelper::\\$outputAllowed) { ?>',
  169. 169:             $s
  170. 170:         );
  171. 171:     }
  172. 172:  
  173. 173:  
  174. 174:  
  175. 175:     /**
  176. 176:      * Finishes parsing.
  177. 177:      * @param  string 
  178. 178:      * @return void 
  179. 179:      */
  180. 180:     public function finalize($s)
  181. 181:     {
  182. 182:         // blocks closing check
  183. 183:         if (count($this->blocks=== 1// auto-close last block
  184. 184:             $s .= $this->macro('/block''''');
  185. 185:  
  186. 186:         elseif ($this->blocks{
  187. 187:             throw new InvalidStateException("There are some unclosed blocks.");
  188. 188:         }
  189. 189:  
  190. 190:         // snippets support (temporary solution)
  191. 191:         $s "<?php\nif (SnippetHelper::\$outputAllowed) {\n?>$s<?php\n}\n?>";
  192. 192:  
  193. 193:         // extends support
  194. 194:         if ($this->namedBlocks || $this->extends{
  195. 195:             $s "<?php\n"
  196. 196:                 . 'if ($_cb->extends) { ob_start(); }' "\n"
  197. 197:                 . '?>' $s "<?php\n"
  198. 198:                 . 'if ($_cb->extends) { ob_end_clean(); CurlyBracketsMacros::includeTemplate($_cb->extends, get_defined_vars(), $template)->render(); }' "\n";
  199. 199:         }
  200. 200:  
  201. 201:         // named blocks
  202. 202:         if ($this->namedBlocks{
  203. 203:             foreach (array_reverse($this->namedBlocksTRUEas $name => $foo{
  204. 204:                 $name preg_quote($name'#');
  205. 205:                 $s preg_replace_callback("#{block($name)} \?>(.*)<\?php {/block$name}#sU"array($this'cbNamedBlocks')$s);
  206. 206:             }
  207. 207:             $s "<?php\n\n" implode("\n\n\n"$this->namedBlocks"\n\n//\n// end of blocks\n//\n?>" $s;
  208. 208:         }
  209. 209:  
  210. 210:         // internal state holder
  211. 211:         $s "<?php\n"
  212. 212:             
  213. 213:             . "\$_cb = CurlyBracketsMacros::initRuntime(\$template, " var_export($this->extendsTRUE", " var_export($this->uniqTRUE"); unset(\$_extends);\n"
  214. 214:             . '?>' $s;
  215. 215:     }
  216. 216:  
  217. 217:  
  218. 218:  
  219. 219:     /**
  220. 220:      * Process {macro content | modifiers}
  221. 221:      * @param  string 
  222. 222:      * @param  string 
  223. 223:      * @param  string 
  224. 224:      * @return string 
  225. 225:      */
  226. 226:     public function macro($macro$content$modifiers)
  227. 227:     {
  228. 228:         if ($macro === ''{
  229. 229:             $macro substr($content02);
  230. 230:             if (!isset($this->macros[$macro])) {
  231. 231:                 $macro substr($content01);
  232. 232:                 if (!isset($this->macros[$macro])) {
  233. 233:                     return NULL;
  234. 234:                 }
  235. 235:             }
  236. 236:             $content substr($contentstrlen($macro));
  237. 237:  
  238. 238:         elseif (!isset($this->macros[$macro])) {
  239. 239:             return NULL;
  240. 240:         }
  241. 241:         $this->current array($content$modifiers);
  242. 242:         return preg_replace_callback('#%(.*?)%#'array($this'cbMacro')$this->macros[$macro]);
  243. 243:     }
  244. 244:  
  245. 245:  
  246. 246:  
  247. 247:     /**
  248. 248:      * Callback for self::macro().
  249. 249:      */
  250. 250:     private function cbMacro($m)
  251. 251:     {
  252. 252:         list($content$modifiers$this->current;
  253. 253:         if ($m[1]{
  254. 254:             $callback $m[1][0=== ':' array($thissubstr($m[1]1)) $m[1];
  255. 255:             fixCallback($callback);
  256. 256:             if (!is_callable($callback)) {
  257. 257:                 $able is_callable($callbackTRUE$textual);
  258. 258:                 throw new InvalidStateException("CurlyBrackets macro handler '$textual' is not ($able 'callable.' 'valid PHP callback.'));
  259. 259:             }
  260. 260:             return call_user_func($callback$content$modifiers);
  261. 261:  
  262. 262:         else {
  263. 263:             return $content;
  264. 264:         }
  265. 265:     }
  266. 266:  
  267. 267:  
  268. 268:  
  269. 269:     /**
  270. 270:      * Process <n:tag attr> (experimental).
  271. 271:      * @param  string 
  272. 272:      * @param  array 
  273. 273:      * @param  bool 
  274. 274:      * @return string 
  275. 275:      */
  276. 276:     public function tagMacro($name$attrs$closing)
  277. 277:     {
  278. 278:         $knownTags array(
  279. 279:             'include' => 'block',
  280. 280:             'for' => 'each',
  281. 281:             'block' => 'name',
  282. 282:             'if' => 'cond',
  283. 283:             'elseif' => 'cond',
  284. 284:         );
  285. 285:         $value isset($knownTags[$name]$attrs[$knownTags[$name]]$attrs[$knownTags[$name]] substr(var_export($attrsTRUE)8-1);
  286. 286:         if ($name === 'block' || $name === 'include'$value '#' $value;
  287. 287:         return $this->macro($closing "/$name$name$valueisset($attrs['modifiers']$attrs['modifiers''');
  288. 288:     }
  289. 289:  
  290. 290:  
  291. 291:  
  292. 292:     /**
  293. 293:      * Process <tag n:attr> (experimental).
  294. 294:      * @param  string 
  295. 295:      * @param  array 
  296. 296:      * @param  bool 
  297. 297:      * @return string 
  298. 298:      */
  299. 299:     public function attrsMacro($code$attrs$closing)
  300. 300:     {
  301. 301:         $knownAttrs array('if''ifset''foreach''for''while''block''snippet');
  302. 302:         foreach ($knownAttrs as $name{
  303. 303:             if (!isset($attrs[$name])) continue;
  304. 304:             $value $attrs[$name];
  305. 305:             if ($name === 'block'$value '#' $value;
  306. 306:             $value $this->macro($closing "/$name$name$value'');
  307. 307:             $code $closing $code $value $value $code;
  308. 308:             unset($attrs[$name]);
  309. 309:         }
  310. 310:         return $attrs NULL $code;
  311. 311:     }
  312. 312:  
  313. 313:  
  314. 314:  
  315. 315:     /********************* macros ****************d*g**/
  316. 316:  
  317. 317:  
  318. 318:  
  319. 319:     /**
  320. 320:      * {$var |modifiers}
  321. 321:      */
  322. 322:     private function macroVar($var$modifiers)
  323. 323:     {
  324. 324:         return CurlyBracketsFilter::formatModifiers('$' $var$modifiers);
  325. 325:     }
  326. 326:  
  327. 327:  
  328. 328:  
  329. 329:     /**
  330. 330:      * {include ...}
  331. 331:      */
  332. 332:     private function macroInclude($content$modifiers)
  333. 333:     {
  334. 334:         $destination CurlyBracketsFilter::fetchToken($content)// destination [,] [params]
  335. 335:         $params CurlyBracketsFilter::formatArray($content($content ' + ' '');
  336. 336:  
  337. 337:         if ($destination === NULL{
  338. 338:             throw new InvalidStateException("Missing destination in {include} on line {$this->filter->line}.");
  339. 339:  
  340. 340:         } elseif ($destination[0] === '#') { // include #block
  341. 341:             if (!preg_match('#^\\#'.CurlyBracketsFilter::RE_IDENTIFIER.'$#', $destination)) {
  342. 342:                 throw new InvalidStateException("Included block name must be alphanumeric string, '$destination' given on line {$this->filter->line}.");
  343. 343:             }
  344. 344:  
  345. 345:             $parent = $destination === '#parent';
  346. 346:             if ($destination === '#parent' || $destination === '#this') {
  347. 347:                 $item = end($this->blocks);
  348. 348:                 while ($item && $item[0][0!== '#'$item prev($this->blocks);
  349. 349:                 if (!$item{
  350. 350:                     throw new InvalidStateException("Cannot include $name block outside of any block on line {$this->filter->line}.");
  351. 351:                 }
  352. 352:                 $destination = $item[0];
  353. 353:             }
  354. 354:             $name = var_export($destination, TRUE);
  355. 355:             $params .= 'get_defined_vars()';
  356. 356:             $cmd = isset($this->namedBlocks[$destination]&& !$parent
  357. 357:                 ? "call_user_func(reset(\$_cb->blocks[$name]), $params)"
  358. 358:                 : "CurlyBracketsMacros::callBlock" . ($parent ? 'Parent' : '') . "(\$_cb->blocks, $name$params)";
  359. 359:             return $modifiers
  360. 360:                 ? "ob_start(); $cmd; echo " . CurlyBracketsFilter::formatModifiers('ob_get_clean()', $modifiers)
  361. 361:                 : $cmd;
  362. 362:  
  363. 363:         } else { // include "file"
  364. 364:             $destination = CurlyBracketsFilter::formatString($destination);
  365. 365:             $params .= '$template->getParams()';
  366. 366:             return $modifiers
  367. 367:                 ? 'echo ' . CurlyBracketsFilter::formatModifiers('CurlyBracketsMacros::includeTemplate(' . $destination . ', ' . $params . ', $_cb->templates[' . var_export($this->uniqTRUE'])->__toString(TRUE)'$modifiers)
  368. 368:                 : 'CurlyBracketsMacros::includeTemplate(' $destination ', ' $params ', $_cb->templates[' var_export($this->uniqTRUE'])->render()';
  369. 369:         }
  370. 370:     }
  371. 371:  
  372. 372:  
  373. 373:  
  374. 374:     /**
  375. 375:      * {extends ...}
  376. 376:      */
  377. 377:     private function macroExtends($content)
  378. 378:     {
  379. 379:         $destination = CurlyBracketsFilter::fetchToken($content); // destination
  380. 380:         if ($destination === NULL) {
  381. 381:             throw new InvalidStateException("Missing destination in {extends} on line {$this->filter->line}.");
  382. 382:         }
  383. 383:         if (!empty($this->blocks)) {
  384. 384:             throw new InvalidStateException("{extends} must be placed outside any block; on line {$this->filter->line}.");
  385. 385:         }
  386. 386:         if ($this->extends !== NULL{
  387. 387:             throw new InvalidStateException("Multiple {extends} declarations are not allowed; on line {$this->filter->line}.");
  388. 388:         }
  389. 389:         $this->extends $destination !== 'none';
  390. 390:         return $this->extends '$_cb->extends = ' CurlyBracketsFilter::formatString($destination'';
  391. 391:     }
  392. 392:  
  393. 393:  
  394. 394:  
  395. 395:     /**
  396. 396:      * {block ...}
  397. 397:      */
  398. 398:     private function macroBlock($content, $modifiers)
  399. 399:     {
  400. 400:         $name = CurlyBracketsFilter::fetchToken($content); // block [,] [params]
  401. 401:  
  402. 402:         if ($name === NULL || $name[0] === '$') { // anonymous block or capture
  403. 403:             $this->blocks[array($name$modifiers);
  404. 404:             return ($name === NULL && $modifiers === '''' 'ob_start()';
  405. 405:  
  406. 406:         } elseif ($name[0] === '#') { // #block
  407. 407:             if (!preg_match('#^\\#'.CurlyBracketsFilter::RE_IDENTIFIER.'$#', $name)) {
  408. 408:                 throw new InvalidStateException("Block name must be alphanumeric string, '$name' given on line {$this->filter->line}.");
  409. 409:  
  410. 410:             } elseif (isset($this->namedBlocks[$name])) {
  411. 411:                 throw new InvalidStateException("Cannot redeclare block '$name'; on line {$this->filter->line}.");
  412. 412:             }
  413. 413:  
  414. 414:             $top = empty($this->blocks);
  415. 415:             $this->namedBlocks[$name$name;
  416. 416:             $this->blocks[array($name'');
  417. 417:             if (!$top{
  418. 418:                 return $this->macroInclude($name$modifiers"{block$name}";
  419. 419:  
  420. 420:             } elseif ($this->extends{
  421. 421:                 return "{block$name}";
  422. 422:  
  423. 423:             } else {
  424. 424:                 return 'if (!$_cb->extends) { ' . $this->macroInclude($name$modifiers"; } {block$name}";
  425. 425:             }
  426. 426:  
  427. 427:         } else {
  428. 428:             throw new InvalidStateException("Invalid block parameter '$name' on line {$this->filter->line}.");
  429. 429:         }
  430. 430:     }
  431. 431:  
  432. 432:  
  433. 433:  
  434. 434:     /**
  435. 435:      * {/block ...}
  436. 436:      */
  437. 437:     private function macroBlockEnd($content)
  438. 438:     {
  439. 439:         $empty = empty($this->blocks);
  440. 440:         list($name$modifiersarray_pop($this->blocks);
  441. 441:  
  442. 442:         if ($empty || ($content && $content !== $name)) {
  443. 443:             throw new InvalidStateException("Tag {/block $content} was not expected here on line {$this->filter->line}.");
  444. 444:  
  445. 445:         } elseif (substr($name, 0, 1) === '#') { // #block
  446. 446:             return "{/block$name}";
  447. 447:  
  448. 448:         } else { // anonymous block or capture
  449. 449:             return ($name === NULL && $modifiers === '') ? ''
  450. 450:                 : ($name === NULL ? 'echo ' : $name . '=') . CurlyBracketsFilter::formatModifiers('ob_get_clean()', $modifiers);
  451. 451:         }
  452. 452:     }
  453. 453:  
  454. 454:  
  455. 455:  
  456. 456:     /**
  457. 457:      * Converts {block#named}...{/block} to functions.
  458. 458:      */
  459. 459:     private function cbNamedBlocks($matches)
  460. 460:     {
  461. 461:         list(, $name, $content) = $matches;
  462. 462:         $func = '_cbb' . substr(md5($this->uniq $name)010'_' preg_replace('#[^a-z0-9_]#i''_'$name);
  463. 463:         $this->namedBlocks[$name"//\n// block $name\n//\n"
  464. 464:             . "if (!function_exists(\$_cb->blocks[" . var_export($name, TRUE) . "][] = '$func')) { function $func() { extract(func_get_arg(0))\n?>$content<?php\n}}";
  465. 465:         return '';
  466. 466:     }
  467. 467:  
  468. 468:  
  469. 469:  
  470. 470:     /**
  471. 471:      * {foreach ...}
  472. 472:      */
  473. 473:     private function macroForeach($content)
  474. 474:     {
  475. 475:         return '$iterator = $_cb->its[] = new SmartCachingIterator(' . preg_replace('# +as +#i', ') as ', $content, 1);
  476. 476:     }
  477. 477:  
  478. 478:  
  479. 479:  
  480. 480:     /**
  481. 481:      * {attr ...}
  482. 482:      */
  483. 483:     private function macroAttr($content)
  484. 484:     {
  485. 485:         return preg_replace('#\)\s+#', ')->', $content . ' ');
  486. 486:     }
  487. 487:  
  488. 488:  
  489. 489:  
  490. 490:     /**
  491. 491:      * {contentType ...}
  492. 492:      */
  493. 493:     private function macroContentType($content)
  494. 494:     {
  495. 495:         if (strpos($content, 'html') !== FALSE) {
  496. 496:             $this->filter->escape = 'TemplateHelpers::escapeHtml';
  497. 497:             $this->filter->context = CurlyBracketsFilter::CONTEXT_TEXT;
  498. 498:  
  499. 499:         } elseif (strpos($content, 'xml') !== FALSE) {
  500. 500:             $this->filter->escape = 'TemplateHelpers::escapeXml';
  501. 501:             $this->filter->context = CurlyBracketsFilter::CONTEXT_NONE;
  502. 502:  
  503. 503:         } elseif (strpos($content, 'javascript') !== FALSE) {
  504. 504:             $this->filter->escape = 'TemplateHelpers::escapeJs';
  505. 505:             $this->filter->context = CurlyBracketsFilter::CONTEXT_NONE;
  506. 506:  
  507. 507:         } elseif (strpos($content, 'css') !== FALSE) {
  508. 508:             $this->filter->escape = 'TemplateHelpers::escapeCss';
  509. 509:             $this->filter->context = CurlyBracketsFilter::CONTEXT_NONE;
  510. 510:  
  511. 511:         } elseif (strpos($content, 'plain') !== FALSE) {
  512. 512:             $this->filter->escape = '';
  513. 513:             $this->filter->context = CurlyBracketsFilter::CONTEXT_NONE;
  514. 514:  
  515. 515:         } else {
  516. 516:             $this->filter->escape = '$template->escape';
  517. 517:             $this->filter->context = CurlyBracketsFilter::CONTEXT_NONE;
  518. 518:         }
  519. 519:  
  520. 520:         // temporary solution
  521. 521:         return strpos($content, '/') ? 'Environment::getHttpResponse()->setHeader("Content-Type", "' . $content . '")' : '';
  522. 522:     }
  523. 523:  
  524. 524:  
  525. 525:  
  526. 526:     /**
  527. 527:      * {dump ...}
  528. 528:      */
  529. 529:     private function macroDump($content)
  530. 530:     {
  531. 531:         return $content ? "array('$content' => $content)" : 'get_defined_vars()';
  532. 532:     }
  533. 533:  
  534. 534:  
  535. 535:  
  536. 536:     /**
  537. 537:      * {snippet ...}
  538. 538:      */
  539. 539:     private function macroSnippet($content)
  540. 540:     {
  541. 541:         $args = array('');
  542. 542:         if ($snippet = CurlyBracketsFilter::fetchToken($content)) {  // [name [,]] [tag]
  543. 543:             $args[] = CurlyBracketsFilter::formatString($snippet);
  544. 544:         }
  545. 545:         if ($content) {
  546. 546:             $args[] = CurlyBracketsFilter::formatString($content);
  547. 547:         }
  548. 548:         return implode(', ', $args);
  549. 549:     }
  550. 550:  
  551. 551:  
  552. 552:  
  553. 553:     /**
  554. 554:      * {widget ...}
  555. 555:      */
  556. 556:     private function macroWidget($content)
  557. 557:     {
  558. 558:         $pair = CurlyBracketsFilter::fetchToken($content); // widget[:method]
  559. 559:         if ($pair === NULL) {
  560. 560:             throw new InvalidStateException("Missing widget name in {widget} on line {$this->filter->line}.");
  561. 561:         }
  562. 562:         $pair = explode(':', $pair, 2);
  563. 563:         $widget = CurlyBracketsFilter::formatString($pair[0]);
  564. 564:         $method = isset($pair[1]) ? ucfirst($pair[1]) : '';
  565. 565:         $method = preg_match('#^('.CurlyBracketsFilter::RE_IDENTIFIER.'|)$#', $method) ? "render$method" : "{\"render$method\"}";
  566. 566:         $param = CurlyBracketsFilter::formatArray($content);
  567. 567:         if (strpos($content, '=>') === FALSE) $param = substr($param, 6, -1); // removes array()
  568. 568:         return ($widget[0] === '$' ? "if (is_object($widget)) {$widget}->$method($param); else " : '')
  569. 569:             . "\$control->getWidget($widget)->$method($param)";
  570. 570:     }
  571. 571:  
  572. 572:  
  573. 573:  
  574. 574:     /**
  575. 575:      * {link ...}
  576. 576:      */
  577. 577:     private function macroLink($content, $modifiers)
  578. 578:     {
  579. 579:         return CurlyBracketsFilter::formatModifiers('$control->link(' . $this->formatLink($content.')'$modifiers);
  580. 580:     }
  581. 581:  
  582. 582:  
  583. 583:  
  584. 584:     /**
  585. 585:      * {plink ...}
  586. 586:      */
  587. 587:     private function macroPlink($content, $modifiers)
  588. 588:     {
  589. 589:         return CurlyBracketsFilter::formatModifiers('$presenter->link(' . $this->formatLink($content.')'$modifiers);
  590. 590:     }
  591. 591:  
  592. 592:  
  593. 593:  
  594. 594:     /**
  595. 595:      * {ifCurrent ...}
  596. 596:      */
  597. 597:     private function macroIfCurrent($content, $modifiers)
  598. 598:     {
  599. 599:         return $content ? CurlyBracketsFilter::formatModifiers('$presenter->link(' . $this->formatLink($content.')'$modifiers'';
  600. 600:     }
  601. 601:  
  602. 602:  
  603. 603:  
  604. 604:     /**
  605. 605:      * Formats {*link ...} parameters.
  606. 606:      */
  607. 607:     private function formatLink($content)
  608. 608:     {
  609. 609:         return CurlyBracketsFilter::formatString(CurlyBracketsFilter::fetchToken($content)) . CurlyBracketsFilter::formatArray($content, ', '); // destination [,] args
  610. 610:     }
  611. 611:  
  612. 612:  
  613. 613:  
  614. 614:     /**
  615. 615:      * {assign ...}
  616. 616:      */
  617. 617:     private function macroAssign($content, $modifiers)
  618. 618:     {
  619. 619:         if (!$content) {
  620. 620:             throw new InvalidStateException("Missing arguments in {assign} on line {$this->filter->line}.");
  621. 621:         }
  622. 622:         if (strpos($content, '=>') === FALSE) { // back compatibility
  623. 623:             return '$' . ltrim(CurlyBracketsFilter::fetchToken($content), '$') . ' = ' . CurlyBracketsFilter::formatModifiers($content === '' ? 'NULL' : $content, $modifiers);
  624. 624:         }
  625. 625:         return 'extract(' . CurlyBracketsFilter::formatArray($content) . ')';
  626. 626:     }
  627. 627:  
  628. 628:  
  629. 629:  
  630. 630:     /**
  631. 631:      * {default ...}
  632. 632:      */
  633. 633:     private function macroDefault($content)
  634. 634:     {
  635. 635:         if (!$content) {
  636. 636:             throw new InvalidStateException("Missing arguments in {default} on line {$this->filter->line}.");
  637. 637:         }
  638. 638:         return 'extract(' . CurlyBracketsFilter::formatArray($content) . ', EXTR_SKIP)';
  639. 639:     }
  640. 640:  
  641. 641:  
  642. 642:  
  643. 643:     /**
  644. 644:      * Escaping helper.
  645. 645:      */
  646. 646:     private function macroEscape($content)
  647. 647:     {
  648. 648:         return $this->filter->escape;
  649. 649:     }
  650. 650:  
  651. 651:  
  652. 652:  
  653. 653:     /**
  654. 654:      * Just modifiers helper.
  655. 655:      */
  656. 656:     private function macroModifiers($content, $modifiers)
  657. 657:     {
  658. 658:         return CurlyBracketsFilter::formatModifiers($content, $modifiers);
  659. 659:     }
  660. 660:  
  661. 661:  
  662. 662:  
  663. 663:     /********************* run-time helpers ****************d*g**/
  664. 664:  
  665. 665:  
  666. 666:  
  667. 667:     /**
  668. 668:      * Calls block.
  669. 669:      * @param  array
  670. 670:      * @param  string
  671. 671:      * @param  array
  672. 672:      * @return void
  673. 673:      */
  674. 674:     public static function callBlock(& $blocks, $name, $params)
  675. 675:     {
  676. 676:         if (empty($blocks[$name])) {
  677. 677:             throw new InvalidStateException("Call to undefined block '$name'.");
  678. 678:         }
  679. 679:         $block = reset($blocks[$name]);
  680. 680:         $block($params);
  681. 681:     }
  682. 682:  
  683. 683:  
  684. 684:  
  685. 685:     /**
  686. 686:      * Calls parent block.
  687. 687:      * @param  array
  688. 688:      * @param  string
  689. 689:      * @param  array
  690. 690:      * @return void
  691. 691:      */
  692. 692:     public static function callBlockParent(& $blocks, $name, $params)
  693. 693:     {
  694. 694:         if (empty($blocks[$name]) || ($block = next($blocks[$name])) === FALSE) {
  695. 695:             throw new InvalidStateException("Call to undefined parent block '$name'.");
  696. 696:         }
  697. 697:         $block($params);
  698. 698:     }
  699. 699:  
  700. 700:  
  701. 701:  
  702. 702:     /**
  703. 703:      * Includes subtemplate.
  704. 704:      * @param  mixed      included file name or template
  705. 705:      * @param  array      parameters
  706. 706:      * @param  ITemplate  current template
  707. 707:      * @return Template
  708. 708:      */
  709. 709:     public static function includeTemplate($destination, $params, $template)
  710. 710:     {
  711. 711:         if ($destination instanceof ITemplate) {
  712. 712:             $tpl = $destination;
  713. 713:  
  714. 714:         } elseif ($destination == NULL) { // intentionally ==
  715. 715:             throw new InvalidArgumentException("Template file name was not specified.");
  716. 716:  
  717. 717:         } else {
  718. 718:             $tpl = clone $template;
  719. 719:             if ($template instanceof IFileTemplate) {
  720. 720:                 if (substr($destination, 0, 1) !== '/' && substr($destination, 1, 1) !== ':') {
  721. 721:                     $destination = dirname($template->getFile()) '/' $destination;
  722. 722:                 }
  723. 723:                 $tpl->setFile($destination);
  724. 724:             }
  725. 725:         }
  726. 726:  
  727. 727:         $tpl->setParams($params)// interface?
  728. 728:         return $tpl;
  729. 729:     }
  730. 730:  
  731. 731:  
  732. 732:  
  733. 733:     /**
  734. 734:      * Initializes state holder $_cb in template.
  735. 735:      * @param  ITemplate
  736. 736:      * @param  bool
  737. 737:      * @param  string
  738. 738:      * @return stdClass
  739. 739:      */
  740. 740:     public static function initRuntime($template, $extends, $realFile)
  741. 741:     {
  742. 742:         $cb = (object) NULL;
  743. 743:  
  744. 744:         // extends support
  745. 745:         if (isset($template->_cb)) {
  746. 746:             $cb->blocks $template->_cb->blocks;
  747. 747:             $cb->templates $template->_cb->templates;
  748. 748:         }
  749. 749:         $cb->templates[$realFile$template;
  750. 750:         $cb->extends is_bool($extends$extends (empty($template->_extendsFALSE $template->_extends);
  751. 751:         unset($template->_cb$template->_extends);
  752. 752:  
  753. 753:         // cache support
  754. 754:         if (!empty($cb->caches)) {
  755. 755:             end($cb->caches)->addFile($template->getFile());
  756. 756:         }
  757. 757:  
  758. 758:         return $cb;
  759. 759:     }
  760. 760: