diff --git a/docs/guide/structure-views.md b/docs/guide/structure-views.md index ee048cf6a1..3363f69836 100644 --- a/docs/guide/structure-views.md +++ b/docs/guide/structure-views.md @@ -127,6 +127,8 @@ Within [controllers](structure-controllers.md), you may call the following contr and injects all registered JS/CSS scripts and files. It is usually used in response to AJAX Web requests. * [[yii\base\Controller::renderFile()|renderFile()]]: renders a view specified in terms of a view file path or [alias](concept-aliases.md). +* [[yii\base\Controller::renderContent()|renderContent()]]: renders a static string by embedding it into + the currently applicable [layout](#layouts). This method is available since version 2.0.1. For example, diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 23d89e2506..8bbaadab63 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -52,6 +52,7 @@ Yii Framework 2 Change Log - Enh #5983: Added `Inflector::sentence()` (pana1990, qiangxue) - Enh: `Console::confirm()` now uses `Console::stdout()` instead of `echo` to be consistent with all other functions (cebe) - Enh: `yii\rbac\DbManager` migration now uses database component specified in component settings instead of always using default `db` (samdark) +- Enh: Added `yii\base\Controller::renderContent()` (qiangxue) - Chg #3630: `yii\db\Command::queryInternal()` is now protected (samdark) - Chg #5508: Dropped the support for the `--append` option for the `fixture` command (qiangxue) - Chg #5874: Upgraded Twitter Bootstrap to 3.3.x (samdark) diff --git a/framework/base/Controller.php b/framework/base/Controller.php index b08fc66c6c..4993b893ef 100644 --- a/framework/base/Controller.php +++ b/framework/base/Controller.php @@ -364,17 +364,29 @@ class Controller extends Component implements ViewContextInterface */ public function render($view, $params = []) { - $output = $this->getView()->render($view, $params, $this); + $content = $this->getView()->render($view, $params, $this); + return $this->renderContent($content); + } + + /** + * Renders a static string by applying a layout. + * @param string $content the static string being rendered + * @return string the rendering result of the layout with the given static string as the `$content` variable. + * If the layout is disabled, the string will be returned back. + * @since 2.0.1 + */ + public function renderContent($content) + { $layoutFile = $this->findLayoutFile($this->getView()); if ($layoutFile !== false) { - return $this->getView()->renderFile($layoutFile, ['content' => $output], $this); + return $this->getView()->renderFile($layoutFile, ['content' => $content], $this); } else { - return $output; + return $content; } } /** - * Renders a view. + * Renders a view without applying layout. * This method differs from [[render()]] in that it does not apply any layout. * @param string $view the view name. Please refer to [[render()]] on how to specify a view name. * @param array $params the parameters (name-value pairs) that should be made available in the view.