2011-08-10 10 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

मैंने इसे स्मार्ट के लिए अपना "आंतरिक" फ़ंक्शन बनाने का हल किया। यह संभवतः स्मार्टी का उपयोग करने का इरादा नहीं हो सकता है, लेकिन यह निश्चित रूप से मेरे लिए काम करता है ... जो मेरे लिए सबसे महत्वपूर्ण बात है।

यह मेरा अंतिम परिणाम है:

<?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 इस ब्लॉक के अंदर चर का उपयोग नहीं कर पा रहे है। हो सकता है कि आप आंतरिक सामग्री को फिर से smarty पर भेज सकें, लेकिन चूंकि यह केवल फ़ंक्शन fetch() में टेम्पलेट फ़ाइलों को अनुमति देता है, मुझे यह सुनिश्चित नहीं है कि यह काम करता है या नहीं।

स्मार्टफ़ी फ़ंक्शन eval हालांकि मददगार क्या हो सकता है, लेकिन मैंने अभी तक इसके साथ काम नहीं किया है।

+0

धन्यवाद! मैंने उस पर ध्यान दिया और कोशिश की, लेकिन जैसा कि आपने समझाया, आंतरिक सामग्री को smarty द्वारा पुन: उपयोग नहीं किया जाता है। एक आवश्यकता कौन सा है। –

+0

मुझे [eval] (http://www.smarty.net/docs/en/language.function.eval.tpl) नामक एक और फ़ंक्शन मिला ... शायद यह आपकी मदद कर सकता है –

+0

आप रजिस्टर_ब्लॉक का उपयोग करना चाहेंगे। मुझे यकीन नहीं है कि इसके भीतर की अन्य स्थिति को कैसे संभालना सबसे अच्छा है, लेकिन कम से कम सामग्री आपके लिए स्मार्ट कोड के रूप में मूल्यांकन करेगा। http://www.smarty.net/docsv2/en/api.register.block.tpl – craigmc

संबंधित मुद्दे