ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Class Phalcon\\Mvc\\View # Class **Phalcon\\Mvc\\View** *extends* abstract class `Phalcon\Di\Injectable` *implements*[*Phalcon\\Events\\EventsAwareInterface*](#), `Phalcon\Di\InjectionAwareInterface`, [*Phalcon\\Mvc\\ViewInterface*](#), [*Phalcon\\Mvc\\ViewBaseInterface*](#) Phalcon\\Mvc\\View is a class for working with the “view” portion of the model-view-controller pattern. That is, it exists to help keep the view script separate from the model and controller scripts. It provides a system of helpers, output filters, and variable escaping. ``` <pre class="calibre14">``` <?php //Setting views directory $view = new \Phalcon\Mvc\View(); $view->setViewsDir('app/views/'); $view->start(); //Shows recent posts view (app/views/posts/recent.phtml) $view->render('posts', 'recent'); $view->finish(); //Printing views output echo $view->getContent(); ``` ``` ### Constants *integer***LEVEL\_MAIN\_LAYOUT** *integer***LEVEL\_AFTER\_TEMPLATE** *integer***LEVEL\_LAYOUT** *integer***LEVEL\_BEFORE\_TEMPLATE** *integer***LEVEL\_ACTION\_VIEW** *integer***LEVEL\_NO\_RENDER** *integer***CACHE\_MODE\_NONE** *integer***CACHE\_MODE\_INVERSE** ### Methods public **getRenderLevel** () ... public **getCurrentRenderLevel** () ... public **getRegisteredEngines** () public **\_\_construct** (\[*array* $options\]) Phalcon\\Mvc\\View constructor public **setViewsDir** (*unknown* $viewsDir) Sets the views directory. Depending of your platform, always add a trailing slash or backslash public **getViewsDir** () Gets views directory public **setLayoutsDir** (*unknown* $layoutsDir) Sets the layouts sub-directory. Must be a directory under the views directory. Depending of your platform, always add a trailing slash or backslash ``` <pre class="calibre14">``` <?php $view->setLayoutsDir('../common/layouts/'); ``` ``` public **getLayoutsDir** () Gets the current layouts sub-directory public **setPartialsDir** (*unknown* $partialsDir) Sets a partials sub-directory. Must be a directory under the views directory. Depending of your platform, always add a trailing slash or backslash ``` <pre class="calibre14">``` <?php $view->setPartialsDir('../common/partials/'); ``` ``` public **getPartialsDir** () Gets the current partials sub-directory public **setBasePath** (*unknown* $basePath) Sets base path. Depending of your platform, always add a trailing slash or backslash ``` <pre class="calibre14">``` <?php $view->setBasePath(__DIR__ . '/'); ``` ``` public **getBasePath** () Gets base path public **setRenderLevel** (*unknown* $level) Sets the render level for the view ``` <pre class="calibre14">``` <?php //Render the view related to the controller only $this->view->setRenderLevel(View::LEVEL_LAYOUT); ``` ``` public <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\View*</a>**disableLevel** (*int|array* $level) Disables a specific level of rendering ``` <pre class="calibre14">``` <?php //Render all levels except ACTION level $this->view->disableLevel(View::LEVEL_ACTION_VIEW); ``` ``` public **setMainView** (*unknown* $viewPath) Sets default view name. Must be a file without extension in the views directory ``` <pre class="calibre14">``` <?php //Renders as main view views-dir/base.phtml $this->view->setMainView('base'); ``` ``` public **getMainView** () Returns the name of the main view public **setLayout** (*unknown* $layout) Change the layout to be used instead of using the name of the latest controller name ``` <pre class="calibre14">``` <?php $this->view->setLayout('main'); ``` ``` public **getLayout** () Returns the name of the main view public <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\View*</a>**setTemplateBefore** (*string|array* $templateBefore) Sets a template before the controller layout public **cleanTemplateBefore** () Resets any “template before” layouts public <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\View*</a>**setTemplateAfter** (*string|array* $templateAfter) Sets a “template after” controller layout public **cleanTemplateAfter** () Resets any template before layouts public <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\View*</a>**setParamToView** (*string* $key, *mixed* $value) Adds parameters to views (alias of setVar) ``` <pre class="calibre14">``` <?php $this->view->setParamToView('products', $products); ``` ``` public <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\View*</a>**setVars** (*array* $params, \[*boolean* $merge\]) Set all the render params ``` <pre class="calibre14">``` <?php $this->view->setVars(array('products' => $products)); ``` ``` public <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\View*</a>**setVar** (*string* $key, *mixed* $value) Set a single view parameter ``` <pre class="calibre14">``` <?php $this->view->setVar('products', $products); ``` ``` public *mixed***getVar** (*string* $key) Returns a parameter previously set in the view public *array***getParamsToView** () Returns parameters to views public *string***getControllerName** () Gets the name of the controller rendered public *string***getActionName** () Gets the name of the action rendered public *array***getParams** () Gets extra parameters of the action rendered public **start** () Starts rendering process enabling the output buffering protected **\_loadTemplateEngines** () Loads registered template engines, if none is registered it will use Phalcon\\Mvc\\View\\Engine\\Php protected **\_engineRender** (*array* $engines, *string* $viewPath, *boolean* $silence, *boolean* $mustClean, \[[*Phalcon\\Cache\\BackendInterface*](#) $cache\]) Checks whether view exists on registered extensions and render it public **registerEngines** (*unknown* $engines) Register templating engines ``` <pre class="calibre14">``` <?php $this->view->registerEngines(array( ".phtml" => "Phalcon\Mvc\View\Engine\Php", ".volt" => "Phalcon\Mvc\View\Engine\Volt", ".mhtml" => "MyCustomEngine" )); ``` ``` public **exists** (*unknown* $view) Checks whether view exists public **render** (*string* $controllerName, *string* $actionName, \[*array* $params\]) Executes render process from dispatching data ``` <pre class="calibre14">``` <?php //Shows recent posts view (app/views/posts/recent.phtml) $view->start()->render('posts', 'recent')->finish(); ``` ``` public <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\View*</a>**pick** (*string|array* $renderView) Choose a different view to render instead of last-controller/last-action ``` <pre class="calibre14">``` <?php class ProductsController extends \Phalcon\Mvc\Controller { public function saveAction() { //Do some save stuff... //Then show the list view $this->view->pick("products/list"); } } ``` ``` public *string***getPartial** (*string* $partialPath, \[*array* $params\]) Renders a partial view ``` <pre class="calibre14">``` <?php //Retrieve the contents of a partial echo $this->getPartial('shared/footer'); ``` ``` ``` <pre class="calibre14">``` <?php //Retrieve the contents of a partial with arguments echo $this->getPartial('shared/footer', array('content' => $html)); ``` ``` public **partial** (*string* $partialPath, \[*array* $params\]) Renders a partial view ``` <pre class="calibre14">``` <?php //Show a partial inside another view $this->partial('shared/footer'); ``` ``` ``` <pre class="calibre14">``` <?php //Show a partial inside another view with parameters $this->partial('shared/footer', array('content' => $html)); ``` ``` public *string***getRender** (*string* $controllerName, *string* $actionName, \[*array* $params\], \[*mixed* $configCallback\]) Perform the automatic rendering returning the output as a string ``` <pre class="calibre14">``` <?php $template = $this->view->getRender('products', 'show', array('products' => $products)); ``` ``` public **finish** () Finishes the render process by stopping the output buffering protected **\_createCache** () Create a Phalcon\\Cache based on the internal cache options public **isCaching** () Check if the component is currently caching the output content public **getCache** () Returns the cache instance used to cache public <a class="calibre6 pcalibre1" href="">*Phalcon\\Mvc\\View*</a>**cache** (\[*boolean|array* $options\]) Cache the actual view render to certain level ``` <pre class="calibre14">``` <?php $this->view->cache(array('key' => 'my-key', 'lifetime' => 86400)); ``` ``` public **setContent** (*unknown* $content) Externally sets the view content ``` <pre class="calibre14">``` <?php $this->view->setContent("<h1>hello</h1>"); ``` ``` public **getContent** () Returns cached output from another view stage public **getActiveRenderPath** () Returns the path of the view that is currently rendered public **disable** () Disables the auto-rendering process public **enable** () Enables the auto-rendering process public **reset** () Resets the view component to its factory default values public **\_\_set** (*string* $key, *mixed* $value) Magic method to pass variables to the views ``` <pre class="calibre14">``` <?php $this->view->products = $products; ``` ``` public *mixed*\*\*\_\_get\*\* (*string* $key) Magic method to retrieve a variable passed to the view ``` <pre class="calibre14">``` <?php echo $this->view->products; ``` ``` public **isDisabled** () Whether automatic rendering is enabled public *boolean*\*\*\_\_isset\*\* (*string* $key) Magic method to retrieve if a variable is set in the view ``` <pre class="calibre14">``` <?php echo isset($this->view->products); ``` ``` public **setDI** (*unknown* $dependencyInjector) inherited from Phalcon\\Di\\Injectable Sets the dependency injector public **getDI** () inherited from Phalcon\\Di\\Injectable Returns the internal dependency injector public **setEventsManager** (*unknown* $eventsManager) inherited from Phalcon\\Di\\Injectable Sets the event manager public **getEventsManager** () inherited from Phalcon\\Di\\Injectable Returns the internal event manager | - [索引](# "总目录") - [下一页](# "Abstract class Phalcon\Mvc\View\Engine") | - [上一页](# "Class Phalcon\Mvc\User\Plugin") | - [API Indice](#) »