source: branches/0.4/web/addons/job_monarch/dwoo/plugins/builtin/processors/pre.smarty_compat.php @ 755

Last change on this file since 755 was 755, checked in by ramonb, 11 years ago
  • add Dwoo
File size: 2.3 KB
Line 
1<?php
2
3/**
4 * Performs some template conversions to allow smarty templates to be used by
5 * the Dwoo compiler.
6 *
7 * This software is provided 'as-is', without any express or implied warranty.
8 * In no event will the authors be held liable for any damages arising from the use of this software.
9 *
10 * @author     Jordi Boggiano <j.boggiano@seld.be>
11 * @copyright  Copyright (c) 2008, Jordi Boggiano
12 * @license    http://dwoo.org/LICENSE   Modified BSD License
13 * @link       http://dwoo.org/
14 * @version    1.0.0
15 * @date       2008-10-23
16 * @package    Dwoo
17 */
18class Dwoo_Processor_smarty_compat extends Dwoo_Processor
19{
20        public function process($input)
21        {
22                list($l, $r) = $this->compiler->getDelimiters();
23
24                $rl = preg_quote($l,'/');
25                $rr = preg_quote($r,'/');
26                $sectionParam = '(?:(name|loop|start|step|max|show)\s*=\s*(\S+))?\s*';
27                $input = preg_replace_callback('/'.$rl.'\s*section '.str_repeat($sectionParam, 6).'\s*'.$rr.'(.+?)(?:'.$rl.'\s*sectionelse\s*'.$rr.'(.+?))?'.$rl.'\s*\/section\s*'.$rr.'/is', array($this, 'convertSection'), $input);
28                $input = str_replace('$smarty.section.', '$smarty.for.', $input);
29
30                $smarty = array
31                (
32                        '/'.$rl.'\s*ldelim\s*'.$rr.'/',
33                        '/'.$rl.'\s*rdelim\s*'.$rr.'/',
34                        '/'.$rl.'\s*\$smarty\.ldelim\s*'.$rr.'/',
35                        '/'.$rl.'\s*\$smarty\.rdelim\s*'.$rr.'/',
36                        '/\$smarty\./',
37                        '/'.$rl.'\s*php\s*'.$rr.'/',
38                        '/'.$rl.'\s*\/php\s*'.$rr.'/',
39                        '/\|(@?)strip(\||'.$rr.')/',
40                        '/'.$rl.'\s*sectionelse\s*'.$rr.'/',
41                );
42
43                $dwoo = array
44                (
45                        '\\'.$l,
46                        $r,
47                        '\\'.$l,
48                        $r,
49                        '$dwoo.',
50                        '<?php ',
51                        ' ?>',
52                        '|$1whitespace$2',
53                        $l.'else'.$r,
54                );
55
56                if (preg_match('{\|@([a-z][a-z0-9_]*)}i', $input, $matches)) {
57                        trigger_error('The Smarty Compatibility Module has detected that you use |@'.$matches[1].' in your template, this might lead to problems as Dwoo interprets the @ operator differently than Smarty, see http://wiki.dwoo.org/index.php/Syntax#The_.40_Operator', E_USER_NOTICE);
58                }
59
60                return preg_replace($smarty, $dwoo, $input);
61        }
62
63        protected function convertSection(array $matches)
64        {
65                $params = array();
66                $index = 1;
67                while (!empty($matches[$index]) && $index < 13) {
68                        $params[$matches[$index]] = $matches[$index+1];
69                        $index += 2;
70                }
71                return str_replace('['.trim($params['name'], '"\'').']', '[$'.trim($params['name'], '"\'').']', $matches[0]);
72        }
73}
Note: See TracBrowser for help on using the repository browser.