📄 smarty_compiler.class.php
字号:
$this->_trigger_fatal_error("registered '$object' is not an object" , $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
} elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) {
$this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'", $this->_current_file, $this->_current_line_no, __FILE__, __LINE__);
} elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) {
if(in_array($obj_comp, $this->_reg_objects[$object][3])) {
if ($start_tag) {
$prefix = "\$this->_tag_stack[] = array('$obj_comp', $args); ";
$prefix .= "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], null, \$this, \$_block_repeat=true); ";
$prefix .= "while (\$_block_repeat) { ob_start();";
$return = null;
$postfix = '';
} else {
$prefix = "\$this->_obj_block_content = ob_get_contents(); ob_end_clean(); ";
$return = "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$this->_obj_block_content, \$this, \$_block_repeat=false)";
$postfix = "} array_pop(\$this->_tag_stack);";
}
} else {
$return = "\$this->_reg_objects['$object'][0]->$obj_comp($args)";
}
} else {
$return = "\$this->_reg_objects['$object'][0]->$obj_comp";
}
if($return != null) {
if($tag_modifier != '') {
$this->_parse_modifiers($return, $tag_modifier);
}
if(!empty($_assign_var)) {
$output = "\$this->assign('" . $this->_dequote($_assign_var) ."', $return);";
} else {
$output = 'echo ' . $return . ';';
$newline = $this->_additional_newline;
}
} else {
$output = '';
}
return '<?php ' . $prefix . $output . $postfix . "?>" . $newline;
}
function _compile_insert_tag($tag_args)
{
$attrs = $this->_parse_attrs($tag_args);
$name = $this->_dequote($attrs['name']);
if (empty($name)) {
$this->_syntax_error("missing insert name", E_USER_ERROR, __FILE__, __LINE__);
}
if (!empty($attrs['script'])) {
$delayed_loading = true;
} else {
$delayed_loading = false;
}
foreach ($attrs as $arg_name => $arg_value) {
if (is_bool($arg_value))
$arg_value = $arg_value ? 'true' : 'false';
$arg_list[] = "'$arg_name' => $arg_value";
}
$this->_add_plugin('insert', $name, $delayed_loading);
$_params = "array('args' => array(".implode(', ', (array)$arg_list)."))";
return "<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.run_insert_handler.php');\necho smarty_core_run_insert_handler($_params, \$this); ?>" . $this->_additional_newline;
}
function _compile_include_tag($tag_args)
{
$attrs = $this->_parse_attrs($tag_args);
$arg_list = array();
if (empty($attrs['file'])) {
$this->_syntax_error("missing 'file' attribute in include tag", E_USER_ERROR, __FILE__, __LINE__);
}
foreach ($attrs as $arg_name => $arg_value) {
if ($arg_name == 'file') {
$include_file = $arg_value;
continue;
} else if ($arg_name == 'assign') {
$assign_var = $arg_value;
continue;
}
if (is_bool($arg_value))
$arg_value = $arg_value ? 'true' : 'false';
$arg_list[] = "'$arg_name' => $arg_value";
}
$output = '<?php ';
if (isset($assign_var)) {
$output .= "ob_start();\n";
}
$output .=
"\$_smarty_tpl_vars = \$this->_tpl_vars;\n";
$_params = "array('smarty_include_tpl_file' => " . $include_file . ", 'smarty_include_vars' => array(".implode(',', (array)$arg_list)."))";
$output .= "\$this->_smarty_include($_params);\n" .
"\$this->_tpl_vars = \$_smarty_tpl_vars;\n" .
"unset(\$_smarty_tpl_vars);\n";
if (isset($assign_var)) {
$output .= "\$this->assign(" . $assign_var . ", ob_get_contents()); ob_end_clean();\n";
}
$output .= ' ?>';
return $output;
}
function _compile_include_php_tag($tag_args)
{
$attrs = $this->_parse_attrs($tag_args);
if (empty($attrs['file'])) {
$this->_syntax_error("missing 'file' attribute in include_php tag", E_USER_ERROR, __FILE__, __LINE__);
}
$assign_var = (empty($attrs['assign'])) ? '' : $this->_dequote($attrs['assign']);
$once_var = (empty($attrs['once']) || $attrs['once']=='false') ? 'false' : 'true';
$arg_list = array();
foreach($attrs as $arg_name => $arg_value) {
if($arg_name != 'file' AND $arg_name != 'once' AND $arg_name != 'assign') {
if(is_bool($arg_value))
$arg_value = $arg_value ? 'true' : 'false';
$arg_list[] = "'$arg_name' => $arg_value";
}
}
$_params = "array('smarty_file' => " . $attrs['file'] . ", 'smarty_assign' => '$assign_var', 'smarty_once' => $once_var, 'smarty_include_vars' => array(".implode(',', $arg_list)."))";
return "<?php require_once(SMARTY_DIR . 'core' . DIRECTORY_SEPARATOR . 'core.smarty_include_php.php');\nsmarty_core_smarty_include_php($_params, \$this); ?>" . $this->_additional_newline;
}
function _compile_section_start($tag_args)
{
$attrs = $this->_parse_attrs($tag_args);
$arg_list = array();
$output = '<?php ';
$section_name = $attrs['name'];
if (empty($section_name)) {
$this->_syntax_error("missing section name", E_USER_ERROR, __FILE__, __LINE__);
}
$output .= "unset(\$this->_sections[$section_name]);\n";
$section_props = "\$this->_sections[$section_name]";
foreach ($attrs as $attr_name => $attr_value) {
switch ($attr_name) {
case 'loop':
$output .= "{$section_props}['loop'] = is_array(\$_loop=$attr_value) ? count(\$_loop) : max(0, (int)\$_loop); unset(\$_loop);\n";
break;
case 'show':
if (is_bool($attr_value))
$show_attr_value = $attr_value ? 'true' : 'false';
else
$show_attr_value = "(bool)$attr_value";
$output .= "{$section_props}['show'] = $show_attr_value;\n";
break;
case 'name':
$output .= "{$section_props}['$attr_name'] = $attr_value;\n";
break;
case 'max':
case 'start':
$output .= "{$section_props}['$attr_name'] = (int)$attr_value;\n";
break;
case 'step':
$output .= "{$section_props}['$attr_name'] = ((int)$attr_value) == 0 ? 1 : (int)$attr_value;\n";
break;
default:
$this->_syntax_error("unknown section attribute - '$attr_name'", E_USER_ERROR, __FILE__, __LINE__);
break;
}
}
if (!isset($attrs['show']))
$output .= "{$section_props}['show'] = true;\n";
if (!isset($attrs['loop']))
$output .= "{$section_props}['loop'] = 1;\n";
if (!isset($attrs['max']))
$output .= "{$section_props}['max'] = {$section_props}['loop'];\n";
else
$output .= "if ({$section_props}['max'] < 0)\n" .
" {$section_props}['max'] = {$section_props}['loop'];\n";
if (!isset($attrs['step']))
$output .= "{$section_props}['step'] = 1;\n";
if (!isset($attrs['start']))
$output .= "{$section_props}['start'] = {$section_props}['step'] > 0 ? 0 : {$section_props}['loop']-1;\n";
else {
$output .= "if ({$section_props}['start'] < 0)\n" .
" {$section_props}['start'] = max({$section_props}['step'] > 0 ? 0 : -1, {$section_props}['loop'] + {$section_props}['start']);\n" .
"else\n" .
" {$section_props}['start'] = min({$section_props}['start'], {$section_props}['step'] > 0 ? {$section_props}['loop'] : {$section_props}['loop']-1);\n";
}
$output .= "if ({$section_props}['show']) {\n";
if (!isset($attrs['start']) && !isset($attrs['step']) && !isset($attrs['max'])) {
$output .= " {$section_props}['total'] = {$section_props}['loop'];\n";
} else {
$output .= " {$section_props}['total'] = min(ceil(({$section_props}['step'] > 0 ? {$section_props}['loop'] - {$section_props}['start'] : {$section_props}['start']+1)/abs({$section_props}['step'])), {$section_props}['max']);\n";
}
$output .= " if ({$section_props}['total'] == 0)\n" .
" {$section_props}['show'] = false;\n" .
"} else\n" .
" {$section_props}['total'] = 0;\n";
$output .= "if ({$section_props}['show']):\n";
$output .= "
for ({$section_props}['index'] = {$section_props}['start'], {$section_props}['iteration'] = 1;
{$section_props}['iteration'] <= {$section_props}['total'];
{$section_props}['index'] += {$section_props}['step'], {$section_props}['iteration']++):\n";
$output .= "{$section_props}['rownum'] = {$section_props}['iteration'];\n";
$output .= "{$section_props}['index_prev'] = {$section_props}['index'] - {$section_props}['step'];\n";
$output .= "{$section_props}['index_next'] = {$section_props}['index'] + {$section_props}['step'];\n";
$output .= "{$section_props}['first'] = ({$section_props}['iteration'] == 1);\n";
$output .= "{$section_props}['last'] = ({$section_props}['iteration'] == {$section_props}['total']);\n";
$output .= "?>";
return $output;
}
function _compile_foreach_start($tag_args)
{
$attrs = $this->_parse_attrs($tag_args);
$arg_list = array();
if (empty($attrs['from'])) {
return $this->_syntax_error("foreach: missing 'from' attribute", E_USER_ERROR, __FILE__, __LINE__);
}
$from = $attrs['from'];
if (empty($attrs['item'])) {
return $this->_syntax_error("foreach: missing 'item' attribute", E_USER_ERROR, __FILE__, __LINE__);
}
$item = $this->_dequote($attrs['item']);
if (!preg_match('~^\w+$~', $item)) {
return $this->_syntax_error("'foreach: item' must be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
}
if (isset($attrs['key'])) {
$key = $this->_dequote($attrs['key']);
if (!preg_match('~^\w+$~', $key)) {
return $this->_syntax_error("foreach: 'key' must to be a variable name (literal string)", E_USER_ERROR, __FILE__, __LINE__);
}
$key_part = "\$this->_tpl_vars['$key'] => ";
} else {
$key = null;
$key_part = '';
}
if (isset($attrs['name'])) {
$name = $attrs['name'];
} else {
$name = null;
}
$output = '<?php ';
if (isset($name)) {
$foreach_props = "\$this->_foreach[$name]";
$output .= "if (isset(\$this->_foreach[$name])) unset(\$this->_foreach[$name]);\n";
$output .= "{$foreach_props}['total'] = count(\$_from = (array)$from);\n";
$output .= "{$foreach_props}['show'] = {$foreach_props}['total'] > 0;\n";
$output .= "if ({$foreach_props}['show']):\n";
$output .= "{$foreach_props}['iteration'] = 0;\n";
$output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
$output .= " {$foreach_props}['iteration']++;\n";
$output .= " {$foreach_props}['first'] = ({$foreach_props}['iteration'] == 1);\n";
$output .= " {$foreach_props}['last'] = ({$foreach_props}['iteration'] == {$foreach_props}['total']);\n";
} else {
$output .= "if (count(\$_from = (array)$from)):\n";
$output .= " foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
}
$output .= '?>';
return $output;
}
function _compile_capture_tag($start, $tag_args = '')
{
$attrs = $this->_parse_attrs($tag_args);
if ($start) {
if (isset($attrs['name']))
$buffer = $attrs['name'];
else
$buffer = "'default'";
if (isset($attrs['assign']))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -