1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to add variable to viewparams

Discussion in 'XenForo Development Discussions' started by DroidHost, Jun 3, 2011.

  1. DroidHost

    DroidHost Well-Known Member

    how to add variable to viewparams of the parent function ...

    and how to access a vriable from the parent ....

    this is my code
    PHP:
    class Demo_ControllerPublic_Forum extends XFCP_Demo_ControllerPublic_Forum
    {
        public function 
    actionIndex()
        {
            
    $visitor XenForo_Visitor::getInstance();
            
    $visitorId XenForo_Visitor::getUserId();
            
    $forumId $this->_input->filterSingle('node_id'XenForo_Input::UINT);

            
    $ftpHelper $this->getHelper('ForumThreadPost');
            
    $forumFetchOptions = array('readUserId' => $visitor['user_id']);
            
    $forum $ftpHelper->assertForumValidAndViewable($forumId $forumId $forumName$forumFetchOptions);

            
    $forumId $forum['node_id'];
            
    $response parent::actionIndex();
            if (
    $forumId == 2)
            {
                
    $mayname 'ali';//array('mayname'=>'Ali');
                //$this->getadv($response );
                
    $response->params['myname'] = array();
                
    $response->params['myname'] += $mayname;
                
    //array array_merge('$params');
                //array_push($params,('myname'=>'Ali') , "raspberry");
                
    return $response;
            }
            else
            {
                return 
    $this->responseNoPermission();
            }
        }

    }
     
  2. DroidHost

    DroidHost Well-Known Member

    did this but nothing
    PHP:
            {
                
    $mayname = array('mayname'=>'Aaaali');
                
    //$this->getadv($response );
                
    $response->viewParams[] .= $mayname;
                
    //array array_merge('$params');
                //array_push($params,('myname'=>'Ali') , "raspberry");
                
    return $response;
            }
     
  3. ragtek

    ragtek Guest

    try $response->params += $mayname instead of .=

    That's not a string, so you can't add new elements with .= ;)
     
  4. DroidHost

    DroidHost Well-Known Member

    something .... it is not allowed to use +
    Fatal error: Unsupported operand types
     
  5. ragtek

    ragtek Guest

    Sorry, i've forgotten something

    $response->params += array('newVar' => $value);
     
  6. DroidHost

    DroidHost Well-Known Member

    I do want to pass a new variable to the view ..... what is the solution ?
     
  7. DroidHost

    DroidHost Well-Known Member

    thanks
    but if this was pass to the view $params = array('mayname'=>'Aaaali');
    what is the name should have ? {$myname} in the template ?
     
  8. James

    James Well-Known Member

    Your array key is your template variable, so:
    array('myname' => 'Aaaali')
    {xen:raw $myname}
     
  9. ragtek

    ragtek Guest

    if you use mayname, then it's {$mayname}

    the params are always 'NAME' => 'VALUE', 'NAME2' => VALUE2

    So, to get value into the template, you need {$NAME}, if you want VALUE2 you need {$NAME2}
     
  10. DroidHost

    DroidHost Well-Known Member

    it is not working
     
  11. James

    James Well-Known Member

    because your code uses mayname not myname
     
    Vodkaholic likes this.
  12. DroidHost

    DroidHost Well-Known Member

    not working ,,, I try it before .. !
    PHP:
                $params = array('mayname'=>'Aaaali');

                
    $response->params += $params;
    (
    'myname'=>'Ali') , "raspberry");
                return 
    $response;
     
  13. DroidHost

    DroidHost Well-Known Member

    wow ... then it was working since ever and because of this stupid mistake ..... it wasnt working ..... eeeeeeeh
     
  14. ragtek

    ragtek Guest

    wtf

    what's
    ('myname'=>'Ali') , "raspberry");
    doing in your code?
     
  15. DroidHost

    DroidHost Well-Known Member

    hhhhh

    it was comment out .... some example code from php manual
     
  16. DroidHost

    DroidHost Well-Known Member

    ok ,,,
    how can I take a variable from the parent ? to the child
     
  17. SheepCow

    SheepCow Well-Known Member

    ragtek's already given you the answer.

    PHP:
    $response->params += array('myname' => 'Ali');
    Then you can use

    Code:
    {$myname}
    In the template.
     
  18. SheepCow

    SheepCow Well-Known Member

    You can fiddle the params in the same way, e.g.

    PHP:
    $response->params['blah'] = 'No';
    Will set the param "blah" to be "No" (it will create it if it didn't actually exist)
     
  19. ragtek

    ragtek Guest

    1. IMHO your whole code is unnecessary.
    You can run the parent action as first, and get all the necessary values!

    Example Code:
    PHP:
    class Ragtek_PC_ControllerAdmin_Page extends
        
    ##workmode##
        
    XFCP_Ragtek_PC_ControllerAdmin_Page
        
    ##workmode end##

        ##developmode##
        
    XenForo_ControllerAdmin_Page
        
    ##developmode end##
    {


        public function 
    actionEdit()
        {
            
    $return parent::actionEdit();
            
            
    $return->params['page']['ragtek_link_navbar'] = $this->_getRouteModel()->haveSection($return->params['page']['title']);
            return 
    $return;
        }
    As you see, i'm accessing here: $this->_getRouteModel()->haveSection($return->params['page']['title']);
    direct the variable.

    You can do the same!
    Your code look like you're extending the forum controller, so you can access the forumId via:

    $response = parent::actionIndex();
    if ($response instanceof XenForo_ControllerResponse_View AND $response->params['forum']['node_id'] == 2){
    $reponse->params += array('myname' => 'Ali');
    }
     
    DroidHost and James like this.
  20. DroidHost

    DroidHost Well-Known Member

    Thanks alots

    -does this mean I dont need to extend forum controller?how to select the place of code execution?
     

Share This Page