2011-08-10 20 views
6

Piszę własną administrację i, oczywiście, korzystam z Smarty. Teraz chciałbym dodać funkcję kontroli dostępu podobną do znacznika {if}.Jak napisać mój własny Smarty If-case

Co chciałbym napisać to:

{userAccess module='MyModule' action='WriteMessage'} 
Yeey.. I can write something. My name is {$myName}. 
{/userAccess} 

ale nie mogę dowiedzieć się, jak. Czy ktoś może wskazać mi właściwy kierunek? Również, jeśli to możliwe, dodaj i {else} do niego. Więc kod byłoby:

{userAccess module='MyModule' action='WriteMessage'} 
Yeey.. I can write something. My name is {$myName}. 
{else} 
Nooo.. :(I don't have access. 
{/userAccess} 

Odpowiedz

1

Rozwiązałem go, tworząc własną "wewnętrzną" funkcję dla smarty. To może nie być sposób, w jaki Smarty zamierzał go użyć, ale na pewno działa dla mnie ... co jest dla mnie najważniejsze.

To mój końcowy wynik:

<?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 } ?>"; 
    } 
} 

Mam nadzieję, że to pomoże każdemu, kto ma tego problemu w przyszłości.

+0

ładne obejście! –

0

można spróbować registerPlugin.

$smarty->registerPlugin("block","userAccess", array('YourClass', 'your_function')); 

i klasa:

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

Problem polega na tym, że nie są tak easiliy stanie używać zmiennych wewnątrz tego bloku. Może znowu możesz wysłać wewnętrzną zawartość do smarty, ale ponieważ pozwala ona tylko na pliki szablonów w funkcji fetch(), nie jestem do końca pewien, czy to działa.

Co może być pomocne w przypadku funkcji smarty eval, ale jeszcze z nią nie współpracowałem.

+0

Dzięki! Spojrzałem na to i spróbowałem, ale tak jak wyjaśniłeś, wewnętrzna treść nie jest odnawiana przez smarty. Który jest wymaganiem. –

+0

Znalazłem inną funkcję o nazwie [eval] (http://www.smarty.net/docs/en/language.function.eval.tpl) ... może to może ci pomóc –

+0

Będziesz chciał użyć register_block. Nie jestem pewien, jak najlepiej radzić sobie z innym warunkiem, ale przynajmniej oceni on zawartość jako kod Smarty. http://www.smarty.net/docsv2/en/api.register.block.tpl – craigmc