如何编写自己的Smarty If语句

7

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

2
我创建了自己的“内部”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 } ?>";
    } 
} 

希望这能帮助未来遇到这个问题的人。


很棒的解决方案,我前几天偶然发现了它,并利用它创建了新的编译级块来解决我的问题。 - Scuzzy

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()中使用模板文件,我不太确定这是否有效。

然而,可能有帮助的是Smarty函数eval,但我还没有使用过它。


谢谢!我看了一下并尝试了,但就像你所解释的那样,Smarty 没有呈现内部内容,这是一个要求。 - Paul Peelen
我发现另一个名为 eval 的函数......也许这可以帮助你。 - Sascha Galley
你需要使用register_block。我不确定如何在其中最好处理else条件,但它至少会为您评估内容作为Smarty代码。http://www.smarty.net/docsv2/en/api.register.block.tpl - craigmc
我再看一眼。到目前为止,我尝试复制foreach函数并将其调整到我的要求。也许可以这样做。 - Paul Peelen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接