gpt4 book ai didi

php - 如何编写我自己的 Smarty If-case

转载 作者:可可西里 更新时间:2023-11-01 13:31:05 26 4
gpt4 key购买 nike

我正在编写自己的管理,当然我正在使用 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}

最佳答案

我解决了这个问题,为 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 } ?>";
}
}

我希望这对将来遇到此问题的任何人有所帮助。

关于php - 如何编写我自己的 Smarty If-case,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7012671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com