2011-08-10 4 views
6

나는 내 자신의 관리를 쓰고 있고, 물론 내가 멋지를 사용하고 있습니다. 이제 {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} 

답변

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/en/api.register.block.tpl – craigmc

관련 문제