2011-08-10 7 views
6

私は自分の管理を書いていますが、もちろんSmartyを使っています。 これで、{if}タグによく似たアクセスチェック機能を追加しました。自分のSmartyのケースを書く方法

私が書きたいことは次のとおりです。

{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} 

答えて

1

私はスマートに自分の "内部"機能を作成して解決しました。これは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 } ?>"; 
    } 
} 

私は、これは将来的にこの問題を持っている誰にも役立ちます願っています:

これは私の最終的な結果です。

+0

すてきな回避策! –

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 ''; 
    } 
} 

ここでの問題は、あなたがそうeasiliyこのブロック内の変数を使用することができないということです。たぶんあなたは再びスマートに内部のコンテンツを送信することができますが、それは関数内のテンプレートファイルのみを許可するのでfetch()これが動作するかどうかはわかりません。

でもスマート機能evalは役に立ちますが、私はまだそれを使っていません。

+0

ありがとうございます!私はそれを見てそれを試しましたが、あなたが説明したように、内的な内容は賢明なものではありません。どちらが必要ですか? –

+0

私は[eval](http://www.smarty.net/docs/en/language.function.eval.tpl)と呼ばれる別の関数を見つけました...おそらくこれはあなたを助けることができます –

+0

あなたはregister_blockを使いたいでしょう。その中のelse条件をどのように処理するのがベストかは分かりませんが、少なくともSmartyコードとして内容を評価します。 http://www.smarty.net/docsv2/ja/api.register.block.tpl – craigmc

関連する問題