summaryrefslogtreecommitdiff
path: root/lib/phptal/PHPTAL/Php/Attribute/TAL/OnError.php
blob: 382d387ea998300af35bdb6aba2e04ca3dcd96b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
 * PHPTAL templating engine
 *
 * PHP Version 5
 *
 * @category HTML
 * @package  PHPTAL
 * @author   Laurent Bedubourg <lbedubourg@motion-twin.com>
 * @author   Kornel Lesiński <kornel@aardvarkmedia.co.uk>
 * @license  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
 * @version  SVN: $Id$
 * @link     http://phptal.org/
 */

/**
 * TAL Specifications 1.4
 *
 *      argument ::= (['text'] | 'structure') expression
 *
 * Example:
 *
 *      <p tal:on-error="string: Error! This paragraph is buggy!">
 *      My name is <span tal:replace="here/SlimShady" />.<br />
 *      (My login name is
 *      <b tal:on-error="string: Username is not defined!"
 *         tal:content="user">Unknown</b>)
 *      </p>
 *
 * @package PHPTAL
 * @subpackage Php.attribute.tal
 * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
 */
class PHPTAL_Php_Attribute_TAL_OnError extends PHPTAL_Php_Attribute
{
    public function before(PHPTAL_Php_CodeWriter $codewriter)
    {
        $codewriter->doTry();
        $codewriter->pushCode('ob_start()');
    }

    public function after(PHPTAL_Php_CodeWriter $codewriter)
    {
        $var = $codewriter->createTempVariable();

        $codewriter->pushCode('ob_end_flush()');
        $codewriter->doCatch('Exception '.$var);
        $codewriter->pushCode('$tpl->addError('.$var.')');
        $codewriter->pushCode('ob_end_clean()');

        $expression = $this->extractEchoType($this->expression);

        $code = $codewriter->evaluateExpression($expression);
        switch ($code) {
            case PHPTAL_Php_TalesInternal::NOTHING_KEYWORD:
                break;

            case PHPTAL_Php_TalesInternal::DEFAULT_KEYWORD:
                $codewriter->pushHTML('<pre class="phptalError">');
                $codewriter->doEcho($var);
                $codewriter->pushHTML('</pre>');
                break;

            default:
                $this->doEchoAttribute($codewriter, $code);
                break;
        }
        $codewriter->doEnd('catch');

        $codewriter->recycleTempVariable($var);
    }
}