Class Phalcon\Mvc\View

extends abstract class Phalcon\Di\Injectable

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

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.

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

final protected _isAbsolutePath (mixed $path)

Checks if a path is absolute or not

public setViewsDir (mixed $viewsDir)

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

public getViewsDir ()

Gets views directory

public setLayoutsDir (mixed $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 getLayoutsDir ()

Gets the current layouts sub-directory

public setPartialsDir (mixed $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 getPartialsDir ()

Gets the current partials sub-directory

public setBasePath (mixed $basePath)

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

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

public getBasePath ()

Gets base path

public setRenderLevel (mixed $level)

Sets the render level for the view

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

public disableLevel (mixed $level)

Disables a specific level of rendering

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

public setMainView (mixed $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 getMainView ()

Returns the name of the main view

public setLayout (mixed $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 getLayout ()

Returns the name of the main view

public setTemplateBefore (mixed $templateBefore)

Sets a template before the controller layout

public cleanTemplateBefore ()

Resets any “template before” layouts

public setTemplateAfter (mixed $templateAfter)

Sets a “template after” controller layout

public cleanTemplateAfter ()

Resets any template before layouts

public setParamToView (mixed $key, mixed $value)

Adds parameters to views (alias of setVar)

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

public setVars (array $params, [mixed $merge])

Set all the render params

  1. <?php
  2. $this->view->setVars(
  3. [
  4. "products" => $products,
  5. ]
  6. );

public setVar (mixed $key, mixed $value)

Set a single view parameter

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

public getVar (mixed $key)

Returns a parameter previously set in the view

public getParamsToView ()

Returns parameters to views

public getControllerName ()

Gets the name of the controller rendered

public getActionName ()

Gets the name of the action rendered

public 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 (array $engines)

Register templating engines

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

public exists (mixed $view)

Checks whether view exists

public render (string $controllerName, string $actionName, [array $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 pick (mixed $renderView)

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

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

public getPartial (mixed $partialPath, [mixed $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(
  4. "shared/footer",
  5. [
  6. "content" => $html,
  7. ]
  8. );

public partial (mixed $partialPath, [mixed $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(
  4. "shared/footer",
  5. [
  6. "content" => $html,
  7. ]
  8. );

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

Perform the automatic rendering returning the output as a string

  1. <?php
  2. $template = $this->view->getRender(
  3. "products",
  4. "show",
  5. [
  6. "products" => $products,
  7. ]
  8. );

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 cache ([mixed $options])

Cache the actual view render to certain level

  1. <?php
  2. $this->view->cache(
  3. [
  4. "key" => "my-key",
  5. "lifetime" => 86400,
  6. ]
  7. );

public setContent (mixed $content)

Externally sets the view content

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

public getContent ()

Returns cached output from another view stage

public getActiveRenderPath ()

Returns the path (or paths) of the views that are 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 (mixed $key, mixed $value)

Magic method to pass variables to the views

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

public __get (mixed $key)

Magic method to retrieve a variable passed to the view

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

public isDisabled ()

Whether automatic rendering is enabled

public __isset (mixed $key)

Magic method to retrieve if a variable is set in the view

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

protected getViewsDirs ()

Gets views directories

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

Sets the dependency injector

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

Returns the internal event manager