Documentation

Class Phalcon\Mvc\View

extends abstract class Phalcon\Di\Injectable

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

Source on GitHub

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.

<?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_NAMESPACE

integer LEVEL_CONTROLLER

integer LEVEL_LAYOUT

integer LEVEL_BEFORE_TEMPLATE

integer LEVEL_ACTION_VIEW

integer LEVEL_NO_RENDER

boolean CACHE_MODE_NONE

boolean CACHE_MODE_INVERSE

Methods

public __construct ([array $options])

Phalcon\Mvc\View constructor

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

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

public string getViewsDir ()

Gets views directory

public Phalcon\Mvc\View setLayoutsDir (string $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

<?php

 $view->setLayoutsDir('../common/layouts/');

public string getLayoutsDir ()

Gets the current layouts sub-directory

public Phalcon\Mvc\View setPartialsDir (string $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

<?php

 $view->setPartialsDir('../common/partials/');

public string getPartialsDir ()

Gets the current partials sub-directory

public Phalcon\Mvc\View setBasePath (string|array $basePath)

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

<?php

    $view->setBasePath(__DIR__ . '/');

public string getBasePath ()

Gets base path

public int getCurrentRenderLevel ()

Returns the render level for the view

public int getRenderLevel ()

Returns the render level for the view

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

Sets the render level for the view

<?php

    //Render the view related to the controller only
    $this->view->setRenderLevel(View::LEVEL_LAYOUT);

public Phalcon\Mvc\View disableLevel (int|array $level)

Disables a specific level of rendering

<?php

 //Render all levels except ACTION level
 $this->view->disableLevel(View::LEVEL_ACTION_VIEW);

public array getDisabledLevels ()

Returns an array with disabled render levels

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

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

<?php

    //Renders as main view views-dir/base.phtml
    $this->view->setMainView('base');

public string getMainView ()

Returns the name of the main view

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

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

<?php

    $this->view->setLayout('main');

public string getLayout ()

Returns the name of the main view

public Phalcon\Mvc\View setTemplateBefore (string|array $templateBefore)

Appends template before controller layout

public Phalcon\Mvc\View cleanTemplateBefore ()

Resets any template before layouts

public Phalcon\Mvc\View setTemplateAfter (string|array $templateAfter)

Appends template after controller layout

public Phalcon\Mvc\View cleanTemplateAfter ()

Resets any template after layouts

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

Adds parameters to views (alias of setVar)

<?php

$this->view->setParamToView('products', $products);

public array getParamsToView ()

Returns parameters to views

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

Set all the render params

<?php

$this->view->setVars(array('products' => $products));

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

Set a single view parameter

<?php

$this->view->setVar('products', $products);

public mixed getVar (string $key)

Returns a parameter previously set in the view

public Phalcon\Mvc\View setControllerName (string $controllerName)

Sets the controller name to be view

public string getControllerName ()

Gets the name of the controller rendered

public Phalcon\Mvc\View setActionName (string $actionName)

Sets the action name to be view

public string getActionName ()

Gets the name of the action rendered

public Phalcon\Mvc\View setParams (array $params)

Sets the extra parameters to be view

public array getParams ()

Gets extra parameters of the action rendered

public setNamespaceName ()

...

public getNamespaceName ()

...

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 ()

Checks whether view exists on registered extensions and render it

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

Register templating engines

<?php

$this->view->registerEngines(array(
  ".phtml" => "Phalcon\Mvc\View\Engine\Php",
  ".volt" => "Phalcon\Mvc\View\Engine\Volt",
  ".mhtml" => "MyCustomEngine"
));

public getRegisteredEngines ()

Returns the registered templating engines

public array getEngines ()

Returns the registered templating engines, if none is registered it will use Phalcon\Mvc\View\Engine\Php

public boolean exists (string $view, [unknown $absolute_path])

Checks whether a view file exists

public Phalcon\Mvc\View render (string $controllerName, string $actionName, [array $params], [unknown $namespace], [Phalcon\Mvc\View\ModelInterface $viewModel])

Executes render process from dispatching data

<?php

 //Shows recent posts view (app/views/posts/recent.phtml)
 $view->start()->render('posts', 'recent')->finish();

public Phalcon\Mvc\View pick (string|array $renderView)

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

<?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 partial (string $partialPath)

Renders a partial view

<?php

    //Show a partial inside another view
    $this->partial('shared/footer');
<?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

<?php

    $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 ([boolean|array $options])

Cache the actual view render to certain level

<?php

  $this->view->cache(array('key' => 'my-key', 'lifetime' => 86400));

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

Externally sets the view content

<?php

$this->view->setContent("<h1>hello</h1>");

public string getContent ()

Returns cached output from another view stage

public startSection (string $name)

Start a new section block

public string stopSection ()

Stop the current section block

public string|null section (string $name, [unknown $defaultValue])

Returns the content for a section block

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 boolean isDisabled ()

Whether automatic rendering is enabled

public Phalcon\Mvc\View enableNamespaceView ()

Enables namespace view render

public Phalcon\Mvc\View disableNamespaceView ()

Disables namespace view render

public Phalcon\Mvc\View enableLowerCase ()

Enables to lower case view path

public Phalcon\Mvc\View disableLowerCase ()

Whether to lower case view path

public Phalcon\Mvc\View setConverter (string $name, callable $converter)

Adds a converter

public callable|null getConverter (unknown $name)

Returns the router converter

public Phalcon\Mvc\View reset ()

Resets the view component to its factory default values

public __set (unknown $property, mixed $value)

Magic method to pass variables to the views

<?php

$this->view->products = $products;

public mixed __get (unknown $property)

Magic method to retrieve a variable passed to the view

<?php

echo $this->view->products;

public boolean __isset (unknown $property)

Magic method to inaccessible a variable passed to the view

<?php

isset($this->view->products)

public insert (unknown $partialPath)

...

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

Sets the dependency injector

public Phalcon\DiInterface getDI ([unknown $error], [unknown $notUseDefault]) inherited from Phalcon\Di\Injectable

Returns the internal dependency injector

public setEventsManager (Phalcon\Events\ManagerInterface $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

public boolean fireEvent (string $eventName, [unknown $data], [unknown $cancelable]) inherited from Phalcon\Di\Injectable

Fires an event, implicitly calls behaviors and listeners in the events manager are notified

public boolean fireEventCancel (string $eventName, [unknown $data], [unknown $cancelable]) inherited from Phalcon\Di\Injectable

Fires an event, implicitly calls behaviors and listeners in the events manager are notified This method stops if one of the callbacks/listeners returns boolean false

public mixed fireEventData (string $eventName, [mixed $data]) inherited from Phalcon\Di\Injectable

Fires an event, return data

public boolean hasService (string $name) inherited from Phalcon\Di\Injectable

Check whether the DI contains a service by a name

public Phalcon\Di\ServiceInterface setService (unknown $name) inherited from Phalcon\Di\Injectable

Sets a service from the DI

public object|null getService (unknown $name) inherited from Phalcon\Di\Injectable

Obtains a service from the DI

public mixed getResolveService (string $name, [unknown $args], [unknown $noerror], [unknown $noshared]) inherited from Phalcon\Di\Injectable

Resolves the service based on its configuration

public attachEvent (string $eventType, Closure $callback) inherited from Phalcon\Di\Injectable

Attach a listener to the events

public __sleep () inherited from Phalcon\Di\Injectable

...

public __debugInfo () inherited from Phalcon\Di\Injectable

...