source: branches/0.4/web/addons/job_monarch/dwoo/plugins/builtin/blocks/template.php @ 755

Last change on this file since 755 was 755, checked in by ramonb, 11 years ago
  • add Dwoo
File size: 2.9 KB
Line 
1<?php
2
3/**
4 * Defines a sub-template that can then be called (even recursively) with the defined arguments
5 * <pre>
6 *  * name : template name
7 *  * rest : list of arguments and optional default values
8 * </pre>
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from the use of this software.
11 *
12 * @author     Jordi Boggiano <j.boggiano@seld.be>
13 * @copyright  Copyright (c) 2008, Jordi Boggiano
14 * @license    http://dwoo.org/LICENSE   Modified BSD License
15 * @link       http://dwoo.org/
16 * @version    1.1.0
17 * @date       2009-07-18
18 * @package    Dwoo
19 */
20class Dwoo_Plugin_template extends Dwoo_Block_Plugin implements Dwoo_ICompilable_Block
21{
22        public function init($name, array $rest = array())
23        {
24        }
25
26        public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type)
27        {
28                $params = $compiler->getCompiledParams($params);
29                $parsedParams = array();
30                if (!isset($params['*']))
31                        $params['*'] = array();
32                foreach ($params['*'] as $param=>$defValue) {
33                        if (is_numeric($param)) {
34                                $param = $defValue;
35                                $defValue = null;
36                        }
37                        $param = trim($param, '\'"');
38                        if (!preg_match('#^[a-z0-9_]+$#i', $param)) {
39                                throw new Dwoo_Compilation_Exception($compiler, 'Function : parameter names must contain only A-Z, 0-9 or _');
40                        }
41                        $parsedParams[$param] = $defValue;
42                }
43                $params['name'] = substr($params['name'], 1, -1);
44                $params['*'] = $parsedParams;
45                $params['uuid'] = uniqid();
46                $compiler->addTemplatePlugin($params['name'], $parsedParams, $params['uuid']);
47                $currentBlock =& $compiler->getCurrentBlock();
48                $currentBlock['params'] = $params;
49                return '';
50        }
51
52        public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content)
53        {
54                $paramstr = 'Dwoo_Core $dwoo';
55                $init = 'static $_callCnt = 0;'."\n".
56                '$dwoo->scope[\' '.$params['uuid'].'\'.$_callCnt] = array();'."\n".
57                '$_scope = $dwoo->setScope(array(\' '.$params['uuid'].'\'.($_callCnt++)));'."\n";
58                $cleanup = '/* -- template end output */ $dwoo->setScope($_scope, true);';
59                foreach ($params['*'] as $param=>$defValue) {
60                        if ($defValue === null) {
61                                $paramstr.=', $'.$param;
62                        } else {
63                                $paramstr.=', $'.$param.' = '.$defValue;
64                        }
65                        $init .= '$dwoo->scope[\''.$param.'\'] = $'.$param.";\n";
66                }
67                $init .= '/* -- template start output */';
68
69                $funcName = 'Dwoo_Plugin_'.$params['name'].'_'.$params['uuid'];
70
71                $search = array(
72                        '$this->charset',
73                        '$this->',
74                        '$this,',
75                );
76                $replacement = array(
77                        '$dwoo->getCharset()',
78                        '$dwoo->',
79                        '$dwoo,',
80                );
81                $content = str_replace($search, $replacement, $content);
82
83                $body = 'if (!function_exists(\''.$funcName."')) {\nfunction ".$funcName.'('.$paramstr.') {'."\n$init".Dwoo_Compiler::PHP_CLOSE.
84                        $prepend.$content.$append.
85                        Dwoo_Compiler::PHP_OPEN.$cleanup."\n}\n}";
86                $compiler->addTemplatePlugin($params['name'], $params['*'], $params['uuid'], $body);
87        }
88}
Note: See TracBrowser for help on using the repository browser.