我正在写自己的管理,当然我正在使用Smarty。 现在,我想要做的是添加一个类似于{if}标签的访问检查功能。
我想写的是:
{userAccess module='MyModule' action='WriteMessage'}
Yeey.. I can write something. My name is {$myName}.
{/userAccess}
但我无法弄清楚如何。有人可以指出我正确的方向吗? 此外,如果可能的话,添加和{else}。所以代码是:
{userAccess module='MyModule' action='WriteMessage'}
Yeey.. I can write something. My name is {$myName}.
{else}
Nooo.. :( I don't have access.
{/userAccess}
答案 0 :(得分:1)
我解决了它为smarty创建我自己的“内部”功能。这可能不是Smarty打算使用它的方式,但它肯定对我有用......这对我来说是最重要的事情。
这是我的最终结果:
<?php
/**
* Smarty Internal Plugin Compile UserAccess
*
* Compiles the {useraccess} {useraccesselse} {/useraccess} tags
*
* @package Smarty
* @subpackage Compiler
* @author Paul Peelen
*/
/**
* Smarty Internal Plugin Compile useraccess Class
*/
class Smarty_Internal_Compile_useraccess extends Smarty_Internal_CompileBase {
// attribute definitions
public $required_attributes = array('module', 'action');
public $optional_attributes = array('userid'); // Not yet implemented
public $shorttag_order = array('module','action','userid');
/**
* Compiles code for the {useraccess} tag
*
* @param array $args array with attributes module parser
* @param object $compiler compiler object
* @param array $parameter array with compilation parameter
* @return string compiled code
*/
public function compile($args, $compiler, $parameter)
{
$this->compiler = $compiler;
$tpl = $compiler->template;
// check and get attributes
$_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;
}
}
/**
* Smarty Internal Plugin Compile UserAccessElse Class
*/
class Smarty_Internal_Compile_UserAccessElse extends Smarty_Internal_CompileBase {
/**
* Compiles code for the {useraccesselse} tag
*
* @param array $args array with attributes module parser
* @param object $compiler compiler object
* @param array $parameter array with compilation parameter
* @return string compiled code
*/
public function compile($args, $compiler, $parameter)
{
$this->compiler = $compiler;
// check and get attributes
$_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 { ?>";
}
}
/**
* Smarty Internal Plugin Compile UserAccessclose Class
*/
class Smarty_Internal_Compile_UserAccessclose extends Smarty_Internal_CompileBase {
/**
* Compiles code for the {/useraccess} tag
*
* @param array $args array with attributes module parser
* @param object $compiler compiler object
* @param array $parameter array with compilation parameter
* @return string compiled code
*/
public function compile($args, $compiler, $parameter)
{
$this->compiler = $compiler;
// check and get attributes
$_attr = $this->_get_attributes($args);
// must endblock be nocache?
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 } ?>";
}
}
我希望这可以帮助将来遇到这个问题的任何人。
答案 1 :(得分:0)
您可以尝试registerPlugin。
$smarty->registerPlugin("block","userAccess", array('YourClass', 'your_function'));
和你的班级:
class YourClass {
static function your_function($params, $content, $smarty, &$repeat, $template) {
$module = $params['module'];
$action = $params['action'];
if ($action == 'WriteMessage' && $user_may_write_message) {
return $content;
else
return '';
}
}
这里的问题是你不太容易在这个块中使用变量。也许你可以再次将内部内容发送给smarty,但由于它只允许函数fetch()
中的模板文件,所以我不确定这是否有效。
有什么可以帮助的是聪明的函数eval,但我还没有使用它。