我创建了自己的“内部”Smarty函数来解决这个问题。虽然这可能不是Smarty的预期使用方式,但对我来说确实很有效...这对我来说是最重要的事情。
这是我的最终结果:
<?php
class Smarty_Internal_Compile_useraccess extends Smarty_Internal_CompileBase {
public $required_attributes = array('module', 'action');
public $optional_attributes = array('userid');
public $shorttag_order = array('module','action','userid');
public function compile($args, $compiler, $parameter)
{
$this->compiler = $compiler;
$tpl = $compiler->template;
$_attr = $this->_get_attributes($args);
$module = $_attr['module'];
$action = $_attr['action'];
if (!is_string($module) || !is_string($action))
{
exit ("ERROR");
}
$this->_open_tag('useraccess', array('useraccess', $this->compiler->nocache, $module, $action));
$this->compiler->nocache = $this->compiler->nocache | $this->compiler->tag_nocache;
$output = "<?php ";
$output .= "\$oAuth = \$GLOBALS['oAuth'];\n";
$output .= " \$_smarty_tpl->tpl_vars[$module] = new Smarty_Variable;\n";
$compiler->local_var[$module] = true;
$output .= " \$_smarty_tpl->tpl_vars[$action] = new Smarty_Variable;\n";
$compiler->local_var[$action] = true;
$output .= "if (\$oAuth->getUserSubRights($action,$module)) {";
$output .= "?>";
return $output;
}
}
class Smarty_Internal_Compile_UserAccessElse extends Smarty_Internal_CompileBase {
public function compile($args, $compiler, $parameter)
{
$this->compiler = $compiler;
$_attr = $this->_get_attributes($args);
list($_open_tag, $nocache, $item, $key) = $this->_close_tag(array('useraccess'));
$this->_open_tag('useraccesselse', array('useraccesselse', $nocache, $item, $key));
return "<?php } else { ?>";
}
}
class Smarty_Internal_Compile_UserAccessclose extends Smarty_Internal_CompileBase {
public function compile($args, $compiler, $parameter)
{
$this->compiler = $compiler;
$_attr = $this->_get_attributes($args);
if ($this->compiler->nocache) {
$this->compiler->tag_nocache = true;
}
list($_open_tag, $this->compiler->nocache, $item, $key) = $this->_close_tag(array('useraccess', 'useraccesselse'));
unset($compiler->local_var[$item]);
if ($key != null) {
unset($compiler->local_var[$key]);
}
return "<?php } ?>";
}
}
希望这能帮助未来遇到这个问题的人。