Хранилища Subversion ant

Редакция

Содержимое файла | Последнее изменение | Открыть журнал | RSS

Редакция Автор № строки Строка
304 alex-w 1
<?php
2
 
3
/**
4
* Smarty Internal Plugin Compile extend
5
*
6
* Compiles the {extend} tag
7
*
8
* @package Smarty
9
* @subpackage Compiler
10
* @author Uwe Tews
11
*/
12
/**
13
* Smarty Internal Plugin Compile extend Class
14
*/
15
class Smarty_Internal_Compile_Extend extends Smarty_Internal_CompileBase {
16
    /**
17
    * Compiles code for the {extend} tag
18
    *
19
    * @param array $args array with attributes from parser
20
    * @param object $compiler compiler object
21
    * @return string compiled code
22
    */
23
    public function compile($args, $compiler)
24
    {
25
        $this->compiler = $compiler;
26
        $this->required_attributes = array('file');
27
        // check and get attributes
28
        $_attr = $this->_get_attributes($args);
29
        $_smarty_tpl = $compiler->template;
30
        // $include_file = '';
31
        eval('$include_file = ' . $_attr['file'] . ';');
32
        // create template object
33
        $_template = new Smarty_Template ($include_file, $this->compiler->smarty, $compiler->template);
34
        // save file dependency
35
        $compiler->template->properties['file_dependency']['F'.abs(crc32($_template->getTemplateFilepath()))] = array($_template->getTemplateFilepath(), $_template->getTemplateTimestamp());
36
        $_old_source = $compiler->template->template_source;
37
        if (preg_match_all('/(' . $this->compiler->smarty->left_delimiter . 'block(.+?)' . $this->compiler->smarty->right_delimiter . ')/', $_old_source, $s, PREG_OFFSET_CAPTURE) !=
38
                preg_match_all('/(' . $this->compiler->smarty->left_delimiter . '\/block(.*?)' . $this->compiler->smarty->right_delimiter . ')/', $_old_source, $c, PREG_OFFSET_CAPTURE)) {
39
            $this->compiler->trigger_template_error(" unmatched {block} {/block} pairs");
40
        }
41
        $block_count = count($s[0]);
42
        for ($i = 0; $i < $block_count; $i++) {
43
            $block_content = str_replace($this->compiler->smarty->left_delimiter . '$smarty.parent' . $this->compiler->smarty->right_delimiter, '%%%%SMARTY_PARENT%%%%',
44
                substr($_old_source, $s[0][$i][1] + strlen($s[0][$i][0]), $c[0][$i][1] - $s[0][$i][1] - strlen($s[0][$i][0])));
45
            $this->saveBlockData($block_content, $s[0][$i][0]);
46
        }
47
        $compiler->template->template_source = $_template->getTemplateSource();
48
        $compiler->abort_and_recompile = true;
49
        return ' ';
50
    }
51
 
52
    protected function saveBlockData($block_content, $block_tag)
53
    {
54
        if (0 == preg_match('/(.?)(name=)([^ ]*)/', $block_tag, $_match)) {
55
            $this->compiler->trigger_template_error("\"" . $block_tag . "\" missing name attribute");
56
        } else {
57
            // compile block content
58
            $_tpl = $this->compiler->smarty->createTemplate('string:' . $block_content);
59
            $_tpl->template_filepath = $this->compiler->template->getTemplateFilepath();
60
            $_tpl->suppressHeader = true;
61
            $_compiled_content = $_tpl->getCompiledTemplate();
62
            unset($_tpl);
63
            $_name = trim($_match[3], "\"'}");
64
 
65
            if (isset($this->compiler->template->block_data[$_name])) {
66
                if (strpos($this->compiler->template->block_data[$_name]['compiled'], '%%%%SMARTY_PARENT%%%%') !== false) {
67
                    $this->compiler->template->block_data[$_name]['compiled'] =
68
                    str_replace('%%%%SMARTY_PARENT%%%%', $_compiled_content, $this->compiler->template->block_data[$_name]['compiled']);
69
                } elseif ($this->compiler->template->block_data[$_name]['mode'] == 'prepend') {
70
                    $this->compiler->template->block_data[$_name]['compiled'] .= $_compiled_content;
71
                } elseif ($this->compiler->template->block_data[$_name]['mode'] == 'append') {
72
                    $this->compiler->template->block_data[$_name]['compiled'] = $_compiled_content . $this->compiler->template->block_data[$_name]['compiled'];
73
                }
74
            } else {
75
                $this->compiler->template->block_data[$_name]['compiled'] = $_compiled_content;
76
            }
77
            if (preg_match('/(.?)(append=true)(.*)/', $block_tag, $_match) != 0) {
78
                $this->compiler->template->block_data[$_name]['mode'] = 'append';
79
            } elseif (preg_match('/(.?)(prepend=true)(.*)/', $block_tag, $_match) != 0) {
80
                $this->compiler->template->block_data[$_name]['mode'] = 'prepend';
81
            } else {
82
                $this->compiler->template->block_data[$_name]['mode'] = 'replace';
83
            }
84
        }
85
    }
86
}
87
 
88
?>