Class Phalcon\Mvc\View

extends abstract class Phalcon\Di\Injectable

implements Phalcon\Events\EventsAwareInterface, Phalcon\Di\InjectionAwareInterface, Phalcon\Mvc\ViewInterface

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.

  1. <?php
  2. //Setting views directory
  3. $view = new \Phalcon\Mvc\View();
  4. $view->setViewsDir('app/views/');
  5. $view->start();
  6. //Shows recent posts view (app/views/posts/recent.phtml)
  7. $view->render('posts', 'recent');
  8. $view->finish();
  9. //Printing views output
  10. 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 ([unknown $options])

Phalcon\Mvc\View constructor

public Phalcon\Mvc\View setViewsDir (unknown $viewsDir)

Sets the views directory. Depending of your platform, always add a trailing slash or backslash

public string getViewsDir ()

Gets views directory

public Phalcon\Mvc\View 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

  1. <?php
  2. $view->setLayoutsDir('../common/layouts/');

public string getLayoutsDir ()

Gets the current layouts sub-directory

public Phalcon\Mvc\View 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

  1. <?php
  2. $view->setPartialsDir('../common/partials/');

public string getPartialsDir ()

Gets the current partials sub-directory

public Phalcon\Mvc\View setBasePath (unknown $basePath)

Sets base path. Depending of your platform, always add a trailing slash or backslash

  1. <?php
  2. $view->setBasePath(__DIR__ . '/');

public Phalcon\Mvc\View setRenderLevel (unknown $level)

Sets the render level for the view

  1. <?php
  2. //Render the view related to the controller only
  3. $this->view->setRenderLevel(View::LEVEL_VIEW);

public Phalcon\Mvc\View disableLevel (unknown $level)

Disables a specific level of rendering

  1. <?php
  2. //Render all levels except ACTION level
  3. $this->view->disableLevel(View::LEVEL_ACTION_VIEW);

public Phalcon\Mvc\View setMainView (unknown $viewPath)

Sets default view name. Must be a file without extension in the views directory

  1. <?php
  2. //Renders as main view views-dir/base.phtml
  3. $this->view->setMainView('base');

public string getMainView ()

Returns the name of the main view

public Phalcon\Mvc\View setLayout (unknown $layout)

Change the layout to be used instead of using the name of the latest controller name

  1. <?php
  2. $this->view->setLayout('main');

public string getLayout ()

Returns the name of the main view

public Phalcon\Mvc\View setTemplateBefore (unknown $templateBefore)

Sets a template before the controller layout

public Phalcon\Mvc\View cleanTemplateBefore ()

Resets any “template before” layouts

public Phalcon\Mvc\View setTemplateAfter (unknown $templateAfter)

Sets a “template after” controller layout

public Phalcon\Mvc\View cleanTemplateAfter ()

Resets any template before layouts

public Phalcon\Mvc\View setParamToView (unknown $key, unknown $value)

Adds parameters to views (alias of setVar)

  1. <?php
  2. $this->view->setParamToView('products', $products);

public Phalcon\Mvc\View setVars (unknown $params, [unknown $merge])

Set all the render params

  1. <?php
  2. $this->view->setVars(array('products' => $products));

public Phalcon\Mvc\View setVar (unknown $key, unknown $value)

Set a single view parameter

  1. <?php
  2. $this->view->setVar('products', $products);

public mixed getVar (unknown $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 Phalcon\Mvc\View start ()

Starts rendering process enabling the output buffering

protected array _loadTemplateEngines ()

Loads registered template engines, if none is registered it will use Phalcon\Mvc\View\Engine\Php

protected _engineRender (unknown $engines, unknown $viewPath, unknown $silence, unknown $mustClean, [Phalcon\Cache\BackendInterface $cache])

Checks whether view exists on registered extensions and render it

public Phalcon\Mvc\View registerEngines (unknown $engines)

Register templating engines

  1. <?php
  2. $this->view->registerEngines(array(
  3. ".phtml" => "Phalcon\Mvc\View\Engine\Php",
  4. ".volt" => "Phalcon\Mvc\View\Engine\Volt",
  5. ".mhtml" => "MyCustomEngine"
  6. ));

public bolean exists (unknown $view)

Checks whether view exists

public Phalcon\Mvc\View render (unknown $controllerName, unknown $actionName, [unknown $params])

Executes render process from dispatching data

  1. <?php
  2. //Shows recent posts view (app/views/posts/recent.phtml)
  3. $view->start()->render('posts', 'recent')->finish();

public Phalcon\Mvc\View pick (unknown $renderView)

Choose a different view to render instead of last-controller/last-action

  1. <?php
  2. class ProductsController extends \Phalcon\Mvc\Controller
  3. {
  4. public function saveAction()
  5. {
  6. //Do some save stuff...
  7. //Then show the list view
  8. $this->view->pick("products/list");
  9. }
  10. }

public string getPartial (unknown $partialPath, [unknown $params])

Renders a partial view

  1. <?php
  2. //Retrieve the contents of a partial
  3. echo $this->getPartial('shared/footer');
  1. <?php
  2. //Retrieve the contents of a partial with arguments
  3. echo $this->getPartial('shared/footer', array('content' => $html));

public partial (unknown $partialPath, [unknown $params])

Renders a partial view

  1. <?php
  2. //Show a partial inside another view
  3. $this->partial('shared/footer');
  1. <?php
  2. //Show a partial inside another view with parameters
  3. $this->partial('shared/footer', array('content' => $html));

public string getRender (unknown $controllerName, unknown $actionName, [unknown $params], [unknown $configCallback])

Perform the automatic rendering returning the output as a string

  1. <?php
  2. $template = $this->view->getRender('products', 'show', array('products' => $products));

public Phalcon\Mvc\View finish ()

Finishes the render process by stopping the output buffering

protected Phalcon\Cache\BackendInterface _createCache ()

Create a Phalcon\Cache based on the internal cache options

public boolean isCaching ()

Check if the component is currently caching the output content

public Phalcon\Cache\BackendInterface getCache ()

Returns the cache instance used to cache

public Phalcon\Mvc\View cache ([unknown $options])

Cache the actual view render to certain level

  1. <?php
  2. $this->view->cache(array('key' => 'my-key', 'lifetime' => 86400));

public Phalcon\Mvc\View setContent (unknown $content)

Externally sets the view content

  1. <?php
  2. $this->view->setContent("<h1>hello</h1>");

public string getContent ()

Returns cached output from another view stage

public string getActiveRenderPath ()

Returns the path of the view that is currently rendered

public Phalcon\Mvc\View disable ()

Disables the auto-rendering process

public Phalcon\Mvc\View enable ()

Enables the auto-rendering process

public Phalcon\Mvc\View reset ()

Resets the view component to its factory default values

public __set (unknown $key, unknown $value)

Magic method to pass variables to the views

  1. <?php
  2. $this->view->products = $products;

public mixed __get (unknown $key)

Magic method to retrieve a variable passed to the view

  1. <?php
  2. echo $this->view->products;

public boolean isDisabled ()

Whether automatic rendering is enabled

public setDI (unknown $dependencyInjector) inherited from Phalcon\Di\Injectable

Sets the dependency injector

public Phalcon\DiInterface 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 Phalcon\Events\ManagerInterface getEventsManager () inherited from Phalcon\Di\Injectable

Returns the internal event manager