📄 smarty_compiler.class.php
字号:
case 'insert':
return $this->_compile_insert_tag($tag_args);
default:
if (isset($this->compiler_funcs[$tag_command])) {
return $this->_compile_compiler_tag($tag_command, $tag_args);
} else if (isset($this->custom_funcs[$tag_command])) {
return $this->_compile_custom_tag($tag_command, $tag_args);
} else {
$this->_syntax_error("unknown tag - '$tag_command'", E_USER_WARNING);
return;
}
}
}
/*======================================================================*\
Function: _compile_compiler_tag
Purpose: compile the custom compiler tag
\*======================================================================*/
function _compile_compiler_tag($tag_command, $tag_args)
{
$function = $this->compiler_funcs[$tag_command];
if (!function_exists($function)) {
$this->_syntax_error("compiler function '$tag_command' is not implemented", E_USER_WARNING);
return;
}
return '<?php ' . $function($tag_args, $this) . ' ?>';
}
/*======================================================================*\
Function: _compile_custom_tag
Purpose: compile custom function tag
\*======================================================================*/
function _compile_custom_tag($tag_command, $tag_args)
{
$function = $this->custom_funcs[$tag_command];
if (!function_exists($function)) {
$this->_syntax_error("custom function '$tag_command' is not implemented", E_USER_WARNING);
return;
}
$arg_list = array();
$attrs = $this->_parse_attrs($tag_args);
foreach ($attrs as $arg_name => $arg_value) {
if (is_bool($arg_value))
$arg_value = $arg_value ? 'true' : 'false';
$arg_list[] = "'$arg_name' => $arg_value";
}
return "<?php $function(array(".implode(',', (array)$arg_list)."), \$this); if(\$this->_extract) { extract(\$this->_tpl_vars); \$this->_extract=false; } ?>";
}
/*======================================================================*\
Function: _compile_insert_tag
Purpose: Compile {insert ...} tag
\*======================================================================*/
function _compile_insert_tag($tag_args)
{
$attrs = $this->_parse_attrs($tag_args);
$name = substr($attrs['name'], 1, -1);
if (empty($name)) {
$this->_syntax_error("missing insert name");
}
foreach ($attrs as $arg_name => $arg_value) {
if (is_bool($arg_value))
$arg_value = $arg_value ? 'true' : 'false';
$arg_list[] = "'$arg_name' => $arg_value";
}
return "<?php echo \$this->_run_insert_handler(array(".implode(', ', (array)$arg_list).")); ?>\n";
}
/*======================================================================*\
Function: _compile_config_load_tag
Purpose: Compile {config_load ...} tag
\*======================================================================*/
function _compile_config_load_tag($tag_args)
{
$attrs = $this->_parse_attrs($tag_args);
if (empty($attrs['file'])) {
$this->_syntax_error("missing 'file' attribute in config_load tag");
}
if (empty($attrs['section'])) {
$attrs['section'] = 'null';
}
$scope = @$this->_dequote($attrs['scope']);
if (!empty($scope)) {
if ($scope != 'local' &&
$scope != 'parent' &&
$scope != 'global') {
$this->_syntax_error("invalid 'scope' attribute value");
}
} else {
if (!empty($attrs['global']) && $attrs['global'])
$scope = 'parent';
else
$scope = 'local';
}
$output = '<?php $this->_config_load(' . $attrs['file'] . ', ' . $attrs['section'] . ", '$scope'); ?>";
return $output;
}
/*======================================================================*\
Function: _compile_include_tag
Purpose: Compile {include ...} tag
\*======================================================================*/
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");
}
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" .
"\$this->_smarty_include(".$include_file.", array(".implode(',', (array)$arg_list)."));\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
Purpose: Compile {include ...} tag
\*======================================================================*/
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");
return false;
}
$this->_parse_file_path($this->trusted_dir, $this->_dequote($attrs['file']), $resource_type, $resource_name);
if ($this->security) {
if( $resource_type != 'file' || !@is_file($resource_name)) {
$this->_syntax_error("include_php: $resource_type: $resource_name is not readable");
return false;
}
if (!$this->_is_trusted($resource_type, $resource_name)) {
$this->_syntax_error("include_php: $resource_type: $resource_name is not trusted");
return false;
}
}
if (!empty($attrs['assign'])) {
$output = "<?php ob_start();\n";
$output .= "include('" . $resource_name . "');\n";
$output .= "\$this->assign(" . $this->_dequote($attrs['assign']).", ob_get_contents()); ob_end_clean();\n?>";
} else {
$output = "<?php include('" . $resource_name . "'); ?>";
}
return $output;
}
/*======================================================================*\
Function: _compile_section_start
Purpose: Compile {section ...} tag
\*======================================================================*/
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");
}
$output .= "if (isset(\$this->_sections[$section_name])) 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($attr_value) ? count($attr_value) : max(0, (int)$attr_value);\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'");
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" .
" {$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" .
" 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 .= "?>";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -