diff --git a/framework/base/Application.php b/framework/base/Application.php index f64e352..40e8437 100644 --- a/framework/base/Application.php +++ b/framework/base/Application.php @@ -359,6 +359,15 @@ class Application extends Module } /** + * Returns the view renderer. + * @return ViewRenderer the view renderer used by this application. + */ + public function getViewRenderer() + { + return $this->getComponent('viewRenderer'); + } + + /** * Sets default path aliases. */ public function registerDefaultAliases() diff --git a/framework/base/View.php b/framework/base/View.php index dccfb26..410e3c5 100644 --- a/framework/base/View.php +++ b/framework/base/View.php @@ -137,13 +137,37 @@ class View extends Component /** * Renders a view file. - * This method will extract the given parameters and include the view file. - * It captures the output of the included view file and returns it as a string. + * + * If a [[ViewRenderer|view renderer]] is installed, this method will try to use the view renderer + * to render the view file. Otherwise, it will simply include the view file, capture its output + * and return it as a string. + * + * @param string $file the view file. + * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file. + * @return string the rendering result + */ + public function renderFile($file, $params = array()) + { + $renderer = Yii::$application->getViewRenderer(); + if ($renderer !== null) { + return $renderer->render($this, $file, $params); + } else { + return $this->renderPhpFile($file, $params); + } + } + + /** + * Renders a view file as a PHP script. + * + * This method treats the view file as a PHP script and includes the file. + * It extracts the given parameters and makes them available in the view file. + * The method captures the output of the included view file and returns it as a string. + * * @param string $_file_ the view file. * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file. * @return string the rendering result */ - public function renderFile($_file_, $_params_ = array()) + public function renderPhpFile($_file_, $_params_ = array()) { ob_start(); ob_implicit_flush(false); diff --git a/framework/base/ViewRenderer.php b/framework/base/ViewRenderer.php new file mode 100644 index 0000000..ecb216d --- /dev/null +++ b/framework/base/ViewRenderer.php @@ -0,0 +1,32 @@ + + * @since 2.0 + */ +abstract class ViewRenderer extends Component +{ + /** + * Renders a view file. + * + * This method is invoked by [[View]] whenever it tries to render a view. + * Child classes must implement this method to render the given view file. + * + * @param View $view the view object used for rendering the file. + * @param string $file the view file. + * @param array $params the parameters to be passed to the view file. + * @return string the rendering result + */ + abstract public function render($view, $file, $params); +} diff --git a/framework/console/View.php b/framework/util/ConsoleColor.php similarity index 99% rename from framework/console/View.php rename to framework/util/ConsoleColor.php index 38f0b94..1fadc40 100644 --- a/framework/console/View.php +++ b/framework/util/ConsoleColor.php @@ -1,6 +1,6 @@ * @since 2.0 */ -class View extends \yii\base\Component +class ConsoleColor { const FG_BLACK = 30; const FG_RED = 31; diff --git a/todo.md b/todo.md index 7517b07..60e37c5 100644 --- a/todo.md +++ b/todo.md @@ -5,6 +5,9 @@ * mongodb (put it under framework/db/mongodb) * key-value-based (should allow storage-specific methods additionally to generic ones) * redis (put it under framework/db/redis or perhaps framework/caching?) +- base + * TwigViewRenderer + * SmartyViewRenderer - logging * WebTarget (TBD after web is in place): should consider using javascript and make it into a toolbar * ProfileTarget (TBD after web is in place): should consider using javascript and make it into a toolbar