php Yii RenderPartial 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19915731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:13:44  来源:igfitidea点击:

Yii RenderPartial Parameters

phpyii

提问by train

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:

我不太明白renderpartial 方法中的第三个和第四个参数(return 和processOutput)是做什么的。这是我在 Yii 的文档中发现的:

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?
  1. 对于“return”参数,它表示它控制是否将结果返回或显示给最终用户。这两件事(返回给用户并显示给用户)不是一回事吗?

-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.

- 例如,我正在尝试通过 ajax 向页面添加内容。服务器回显一个 json 编码的 renderpartial 语句,客户端的 javascript 使用 jquery 方法插入它。当我将“return”参数设置为 false 时,整个 ajax 操作都有效并且这些东西成功插入到我指定的位置。但是,当我将“return”参数设置为 true 时,服务器将代码回显为纯文本而不是 html。客户端的 javascript 然后抱怨几个错误......这对我来说根本没有任何意义。

  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?
  1. 什么是后处理?这是在哪里指定的?我知道我没有编写任何“后期处理”,那么这是从哪里来的?

Any help would be much appreciated.

任何帮助将非常感激。

回答by Alexander Kuzmin

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

返回选项选择代码回声是否输出。如果$return = true;这意味着您必须自己取出字符串并回显出来。基本上它必须写之间的区别

<?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 requestMost often, you won't use this feature, and you shouldn't worry about it :)

至于 $processOutput 变量,它用于在返回之前在 html 上调用 $this->processOutput。例如,这可用于在 ajax 请求上生成 hacky 解决方案,请看这里:http: //www.yiiframework.com/forum/index.php/topic/24927-yii-processoutput-only-for-certain-action /和这里Yii renderpartial (proccessoutput = true) 避免重复的 js 请求大多数情况下,您不会使用此功能,也不用担心 :)

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)

(在这里找到:http: //www.yiiframework.com/doc/api/1.1/CController#renderPartial-detail

回答by MD. Sahib Bin Mahboob

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 beforebefore 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 trueit 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 $outputis 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.

  1. $return paremeter:它定义您是否要将呈现页面的输出发送给客户端(请求页面的人)。这是它的工作原理:

    <?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 is will outputted before在页面中的所有 HTML 之前输出,正如我们所说的那样使用输出ob_start()而不是将其发送到浏览器(客户端)并检索它ob_get_contents ()并将输出存储在$output. 我们可以使用代码的最后两行来演示这个东西。

    如果你传递第三个参数,true它会做类似的事情。它将消耗输出并将其作为字符串返回。因此,您可以在字符串中捕获输出。

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

    您可以在此处了解有关 php 中输出控制的更多信息: Output Buffering Controll

  2. $processOutput 参数:如果你传递参数 true 它将调用 的processOutput($output)函数CController,这里$output是你设置的 php 页面呈现的内容。默认情况下它不会被调用renderPartial。它被称为render()renderText()方法CController.引号形式的文档

    对 render() 生成的输出进行后处理。该方法在 render() 和 renderText() 结束时调用。如果有已注册的客户端脚本,此方法会将它们插入到输出的适当位置。如果有动态内容,它们也会被插入。该方法还可以将持久化的页面状态保存在页面中的有状态表单的隐藏字段中。

    简而言之,您只需控制此函数是否会被第 4 个参数调用。

Hope that helps :)

希望有帮助:)