3 * Smarty Internal Plugin Compile Block
5 * Compiles the {block}{/block} tags
13 * Smarty Internal Plugin Compile Block Class
15 class Smarty_Internal_Compile_Block extends Smarty_Internal_CompileBase {
16 // attribute definitions
17 public $required_attributes = array('name');
18 public $shorttag_order = array('name');
20 * Compiles code for the {block} tag
22 * @param array $args array with attributes from parser
23 * @param object $compiler compiler object
24 * @return boolean true
26 public function compile($args, $compiler)
28 $this->compiler = $compiler;
29 // check and get attributes
30 $_attr = $this->_get_attributes($args);
31 $save = array($_attr, $compiler->parser->current_buffer, $this->compiler->nocache, $this->compiler->smarty->merge_compiled_includes, $compiler->smarty->inheritance);
32 $this->_open_tag('block', $save);
33 if ($_attr['nocache'] == true) {
34 $compiler->nocache = true;
36 // set flag for {block} tag
37 $compiler->smarty->inheritance = true;
38 // must merge includes
39 $this->compiler->smarty->merge_compiled_includes = true;
41 $compiler->parser->current_buffer = new _smarty_template_buffer($compiler->parser);
42 $compiler->has_code = false;
47 static function saveBlockData($block_content, $block_tag, $template, $filepath)
49 $_rdl = preg_quote($template->smarty->right_delimiter);
50 $_ldl = preg_quote($template->smarty->left_delimiter);
52 if (0 == preg_match("!({$_ldl}block\s+)(name=)?(\w+|'.*'|\".*\")(\s*?)?((append|prepend|nocache)(=true)?)?(\s*{$_rdl})!", $block_tag, $_match)) {
53 $error_text = 'Syntax Error in template "' . $template->getTemplateFilepath() . '" "' . htmlspecialchars($block_tag) . '" illegal options';
54 throw new SmartyCompilerException($error_text);
56 $_name = trim($_match[3], '\'"');
57 // replace {$smarty.block.child}
58 if (strpos($block_content, $template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter) !== false) {
59 if (isset($template->block_data[$_name])) {
60 $block_content = str_replace($template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter,
61 $template->block_data[$_name]['source'], $block_content);
62 unset($template->block_data[$_name]);
64 $block_content = str_replace($template->smarty->left_delimiter . '$smarty.block.child' . $template->smarty->right_delimiter,
68 if (isset($template->block_data[$_name])) {
69 if (strpos($template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
70 $template->block_data[$_name]['source'] =
71 str_replace('%%%%SMARTY_PARENT%%%%', $block_content, $template->block_data[$_name]['source']);
72 } elseif ($template->block_data[$_name]['mode'] == 'prepend') {
73 $template->block_data[$_name]['source'] .= $block_content;
74 } elseif ($template->block_data[$_name]['mode'] == 'append') {
75 $template->block_data[$_name]['source'] = $block_content . $template->block_data[$_name]['source'];
78 $template->block_data[$_name]['source'] = $block_content;
80 if ($_match[6] == 'append') {
81 $template->block_data[$_name]['mode'] = 'append';
82 } elseif ($_match[6] == 'prepend') {
83 $template->block_data[$_name]['mode'] = 'prepend';
85 $template->block_data[$_name]['mode'] = 'replace';
87 $template->block_data[$_name]['file'] = $filepath;
91 static function compileChildBlock ($compiler, $_name = null)
94 // if called by {$smarty.block.child} we must search the name of enclosing {block}
96 $stack_count = count($compiler->_tag_stack);
97 while (--$stack_count >= 0) {
98 if ($compiler->_tag_stack[$stack_count][0] == 'block') {
99 $_name = trim($compiler->_tag_stack[$stack_count][1][0]['name'] ,"'\"");
103 // flag that child is already compile by {$smarty.block.child} inclusion
104 $compiler->template->block_data[$_name]['compiled'] = true;
106 if ($_name == null) {
107 $compiler->trigger_template_error('{$smarty.block.child} used out of context', $this->compiler->lex->taglineno);
110 if (!isset($compiler->template->block_data[$_name])) {
113 $_tpl = new Smarty_Internal_template ('eval:' . $compiler->template->block_data[$_name]['source'], $compiler->smarty, $compiler->template, $compiler->template->cache_id,
114 $compiler->template->compile_id = null, $compiler->template->caching, $compiler->template->cache_lifetime);
115 $_tpl->properties['nocache_hash'] = $compiler->template->properties['nocache_hash'];
116 $_tpl->template_filepath = $compiler->template->block_data[$_name]['file'];
117 if ($compiler->nocache) {
118 $_tpl->forceNocache = 2;
120 $_tpl->forceNocache = 1;
122 $_tpl->suppressHeader = true;
123 $_tpl->suppressFileDependency = true;
124 if (strpos($compiler->template->block_data[$_name]['source'], '%%%%SMARTY_PARENT%%%%') !== false) {
125 $_output = str_replace('%%%%SMARTY_PARENT%%%%', $compiler->parser->current_buffer->to_smarty_php(), $_tpl->getCompiledTemplate());
126 } elseif ($compiler->template->block_data[$_name]['mode'] == 'prepend') {
127 $_output = $_tpl->getCompiledTemplate() . $compiler->parser->current_buffer->to_smarty_php();
128 } elseif ($compiler->template->block_data[$_name]['mode'] == 'append') {
129 $_output = $compiler->parser->current_buffer->to_smarty_php() . $_tpl->getCompiledTemplate();
130 } elseif (!empty($compiler->template->block_data[$_name])) {
131 $_output = $_tpl->getCompiledTemplate();
133 $compiler->template->properties['file_dependency'] = array_merge($compiler->template->properties['file_dependency'], $_tpl->properties['file_dependency']);
134 $compiler->template->properties['function'] = array_merge($compiler->template->properties['function'], $_tpl->properties['function']);
135 if ($_tpl->has_nocache_code) {
136 $compiler->template->has_nocache_code = true;
138 foreach($_tpl->required_plugins as $code => $tmp1) {
139 foreach($tmp1 as $name => $tmp) {
140 foreach($tmp as $type => $data) {
141 $compiler->template->required_plugins[$code][$name][$type] = $data;
152 * Smarty Internal Plugin Compile BlockClose Class
154 class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_CompileBase {
156 * Compiles code for the {/block} tag
158 * @param array $args array with attributes from parser
159 * @param object $compiler compiler object
160 * @return string compiled code
162 public function compile($args, $compiler)
164 $this->compiler = $compiler;
165 $this->smarty = $compiler->smarty;
166 $this->compiler->has_code = true;
167 // check and get attributes
168 $_attr = $this->_get_attributes($args);
169 $saved_data = $this->_close_tag(array('block'));
170 $_name = trim($saved_data[0]['name'], "\"'");
171 if (isset($compiler->template->block_data[$_name]) && !isset($compiler->template->block_data[$_name]['compiled'])) {
172 $_output = Smarty_Internal_Compile_Block::compileChildBlock($compiler, $_name);
174 $_output = $compiler->parser->current_buffer->to_smarty_php();
175 unset ($compiler->template->block_data[$_name]['compiled']);
178 $compiler->parser->current_buffer = $saved_data[1];
179 $compiler->nocache = $saved_data[2];
180 $compiler->smarty->merge_compiled_includes = $saved_data[3];
181 $compiler->smarty->inheritance = $saved_data[4];
182 // $_output content has already nocache code processed
183 $compiler->suppressNocacheProcessing = true;