Yii difference between rendering functions

2019-01-23 14:38发布

I sometimes get messed up by the three rendering methods:

  • Controller::render()
  • Controller::renderPartial()
  • Controller::renderFile()

Please could you explain. Thank you!

3条回答
疯言疯语
2楼-- · 2019-01-23 15:09

render() is commonly used to render a view that corresponds to what a user sees as a "page" in your application. It first renders the view you have specified and then renders the layout for the current controller action (if applicable), placing the result of the first render into the layout. It then performs output processing (which at this time means automatically inserting any necessary <script> tags and updating dynamic content) and finally outputs the result.

renderPartial() is commonly used to render a "piece" of a page. The main difference from render() is that this method does not place the results of the render in a layout. By default it also does not perform output processing, but you can override this behavior using the $processOutput parameter.

renderFile() is a low-level method that does the grunt work of rendering: it extracts the data variables in the current scope and then runs the view code. The other two methods internally call this one, but you should practically never need to call it yourself. If you do, keep in mind that you need to pass in a file path (not a view path).

查看更多
\"骚年 ilove
3楼-- · 2019-01-23 15:13

renderPartial() is really useful for displaying ssi components in a page - ie, headers, footers, widgets etc.

查看更多
爷、活的狠高调
4楼-- · 2019-01-23 15:20

Render File:

Will run the rendering methods on a given file with the set rendering engine. This is fairly low level within Yii and only really used internally or in console commands.

Render Partial:

This takes the alias given and converts it into a file path using all the local variables such as current running controllers and modules and alias definitions. It then pretty much just uses render file.

Render:

This is combination of render partials to make our lives easier. It will render the layout on the currently active contoller, or the defined one, render all the content within it, handle caching of renders, and process the output for client scripts.

Hope that clears it up.

查看更多
登录 后发表回答