3

I don't quite understand what the third and fourth parameters (return and processOutput) do in the renderpartial method. This is what I found on Yii's documentation:

public string renderPartial(string $view, array $data=NULL, boolean $return=false, boolean $processOutput=false)
 - $view    (string)    name of the view to be rendered. See getViewFile for details about how the view script is resolved.
 - $data    (array)     data to be extracted into PHP variables and made available to the view script
 - $return  (boolean)   whether the rendering result should be returned instead of being displayed to end users
 - $processOutput   (boolean)   whether the rendering result should be postprocessed using processOutput.

I've looked around but can't seem to get what exactly this documentation is trying to say.

  1. For the "return" parameter, it says that it controls whether or not the result is returned or displayed to end users. Aren't these two things (returning something to the user and displaying to the user) the exact same thing?

-for example, I am trying to add stuff to a page by ajax. The server echos a json encoded renderpartial statement and the javascript on the client side inserts it by using jquery methods. When I set the "return" parameter to false, this entire ajax operation works and the stuff is successfully inserted into the location I specify. However, when I set the "return" parameter to true, the server echos the code as merely text and not html. The javascript on the client side then complains of several errors...This doesn't make any sense to me at all.

  1. What is postprocessing and where is this specified? I know that I didn't code up any "post processing", so where is this coming from?

Any help would be much appreciated.

train
  • 610
  • 2
  • 11
  • 20

2 Answers2

4

The return option selects wheather or not the code echo's out or not. If $return = true; this means you'll have to take the string and echo it out yourself. Basically its the difference between having to write

<?php $this->renderPartial($view, $data, false); ?>

and

<?php echo $this->renderPartial($view, $data, true); ?>

As for the $processOutput variable, it's used for calling $this->processOutput on the html before it's returned. This could be used to produce hacky solutions on ajax requests for example, take a look here: http://www.yiiframework.com/forum/index.php/topic/24927-yii-processoutput-only-for-certain-action/ and here Yii renderpartial (proccessoutput = true) Avoid Duplicate js request Most often, you won't use this feature, and you shouldn't worry about it :)

If it makes anything clearer, here's the related source code:

if($processOutput)
    $output=$this->processOutput($output);

if($return)
    return $output;
else
    echo $output;

(found here: http://www.yiiframework.com/doc/api/1.1/CController#renderPartial-detail)

Community
  • 1
  • 1
Alexander Kuzmin
  • 1,120
  • 8
  • 16
3

Lets answer one by one.

  1. $return paremeter : It defines you want to send the output of the rendered page to client (who requested for the page) or not. This is how it works :

    <?php
    ob_start();
    ?>
    <html>
    <body>
    <p>It's like comparing apples to oranges.</p>
    </body>
    </html>
    <?php
    $output=ob_get_contents ();
    echo "This is will outputted before";
    echo $output;
    ?>
    

    This will outputThis is will outputted before before all the HTML in the page as we said to consume the output with ob_start() rather than sending it to browser(client) and retrieving it with ob_get_contents () and storing the output in $output. We can demonstrate the thing using the last two lines of the code.

    If you pass the third parameter true it will do the similar thing. It will consume the output and return it as a string. So you can catch the output in a string.

      $output=$this->renderPartial('a_view.php',$data,true);
      //this line will generate the output
      echo $output;
    

    You can know more about output control in php here: Output Buffering Controll

  2. $processOutput parameter : If you pass the parameter true it will call the processOutput($output) function of CController , here $output is the content rendered form the php page you you set. By default it does not get called in renderPartial. It gets called in render() and renderText() method of CController. Quoted form the doc :

    Postprocesses the output generated by render(). This method is invoked at the end of render() and renderText(). If there are registered client scripts, this method will insert them into the output at appropriate places. If there are dynamic contents, they will also be inserted. This method may also save the persistent page states in hidden fields of stateful forms in the page.

    In plain word,you just controlls if this funciton will get called or not by the 4th parameter.

Hope that helps :)

MD. Sahib Bin Mahboob
  • 20,246
  • 2
  • 23
  • 45