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.
<?php
use Phalcon\Mvc\View;
$view = new View();
// Setting views directory
$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
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
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
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
public getBasePath ()
Gets base path
public setRenderLevel (mixed $level)
Sets the render level for the view
<?php
// Render the view related to the controller only
$this->view->setRenderLevel(
View::LEVEL_LAYOUT
);
public disableLevel (mixed $level)
Disables a specific level of rendering
<?php
// Render all levels except ACTION level
$this->view->disableLevel(
View::LEVEL_ACTION_VIEW
);
public setMainView (mixed $viewPath)
Sets default view name. Must be a file without extension in the views directory
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
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)
public setVars (array $params, [mixed $merge])
Set all the render params
public setVar (mixed $key, mixed $value)
Set a single view parameter
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
<?php
$this->view->registerEngines(
[
".phtml" => "Phalcon\Mvc\View\Engine\Php",
".volt" => "Phalcon\Mvc\View\Engine\Volt",
".mhtml" => "MyCustomEngine",
]
);
public exists (mixed $view)
Checks whether view exists
public render (string $controllerName, string $actionName, [array $params])
Executes render process from dispatching data
<?php
// Shows recent posts view (app/views/posts/recent.phtml)
$view->start()->render("posts", "recent")->finish();
public pick (mixed $renderView)
Choose a different view to render instead of last-controller/last-action
<?php
use Phalcon\Mvc\Controller;
class ProductsController extends Controller
{
public function saveAction()
{
// Do some save stuff...
// Then show the list view
$this->view->pick("products/list");
}
}
public getPartial (mixed $partialPath, [mixed $params])
Renders a partial view
<?php
// Retrieve the contents of a partial with arguments
echo $this->getPartial(
"shared/footer",
[
"content" => $html,
]
);
public partial (mixed $partialPath, [mixed $params])
Renders a partial view
<?php
// Show a partial inside another view with parameters
$this->partial(
"shared/footer",
[
"content" => $html,
]
);
public string getRender (string $controllerName, string $actionName, [array $params], [mixed $configCallback])
Perform the automatic rendering returning the output as a string
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
public setContent (mixed $content)
Externally sets the view content
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
public __get (mixed $key)
Magic method to retrieve a variable passed to the view
public isDisabled ()
Whether automatic rendering is enabled
public __isset (mixed $key)
Magic method to retrieve if a variable is set in the view
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
Abstract class Phalcon\Mvc\View\Engine¶
extends abstract class Phalcon\Di\Injectable
implements Phalcon\Events\EventsAwareInterface, Phalcon\Di\InjectionAwareInterface, Phalcon\Mvc\View\EngineInterface
All the template engine adapters must inherit this class. This provides basic interfacing between the engine and the Phalcon\Mvc\View component.
Methods¶
public __construct (Phalcon\Mvc\ViewBaseInterface $view, [Phalcon\DiInterface $dependencyInjector])
Phalcon\Mvc\View\Engine constructor
public getContent ()
Returns cached output on another view stage
public string partial (string $partialPath, [array $params])
Renders a partial inside another view
public getView ()
Returns the view component related to the adapter
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
public __get (mixed $propertyName) inherited from Phalcon\Di\Injectable
Magic method __get
abstract public render (mixed $path, mixed $params, [mixed $mustClean]) inherited from Phalcon\Mvc\View\EngineInterface
...
Class Phalcon\Mvc\View\Engine\Php¶
extends abstract class Phalcon\Mvc\View\Engine
implements Phalcon\Mvc\View\EngineInterface, Phalcon\Di\InjectionAwareInterface, Phalcon\Events\EventsAwareInterface
Adapter to use PHP itself as templating engine
Methods¶
public render (mixed $path, mixed $params, [mixed $mustClean])
Renders a view using the template engine
public __construct (Phalcon\Mvc\ViewBaseInterface $view, [Phalcon\DiInterface $dependencyInjector]) inherited from Phalcon\Mvc\View\Engine
Phalcon\Mvc\View\Engine constructor
public getContent () inherited from Phalcon\Mvc\View\Engine
Returns cached output on another view stage
public string partial (string $partialPath, [array $params]) inherited from Phalcon\Mvc\View\Engine
Renders a partial inside another view
public getView () inherited from Phalcon\Mvc\View\Engine
Returns the view component related to the adapter
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
public __get (mixed $propertyName) inherited from Phalcon\Di\Injectable
Magic method __get
Class Phalcon\Mvc\View\Engine\Volt¶
extends abstract class Phalcon\Mvc\View\Engine
implements Phalcon\Mvc\View\EngineInterface, Phalcon\Di\InjectionAwareInterface, Phalcon\Events\EventsAwareInterface
Designer friendly and fast template engine for PHP written in Zephir/C
Methods¶
public setOptions (array $options)
Set Volt's options
public getOptions ()
Return Volt's options
public getCompiler ()
Returns the Volt's compiler
public render (mixed $templatePath, mixed $params, [mixed $mustClean])
Renders a view using the template engine
public length (mixed $item)
Length filter. If an array/object is passed a count is performed otherwise a strlen/mb_strlen
public isIncluded (mixed $needle, mixed $haystack)
Checks if the needle is included in the haystack
public convertEncoding (mixed $text, mixed $from, mixed $to)
Performs a string conversion
public slice (mixed $value, [mixed $start], [mixed $end])
Extracts a slice from a string/array/traversable object value
public sort (array $value)
Sorts an array
public callMacro (mixed $name, [array $arguments])
Checks if a macro is defined and calls it
public __construct (Phalcon\Mvc\ViewBaseInterface $view, [Phalcon\DiInterface $dependencyInjector]) inherited from Phalcon\Mvc\View\Engine
Phalcon\Mvc\View\Engine constructor
public getContent () inherited from Phalcon\Mvc\View\Engine
Returns cached output on another view stage
public string partial (string $partialPath, [array $params]) inherited from Phalcon\Mvc\View\Engine
Renders a partial inside another view
public getView () inherited from Phalcon\Mvc\View\Engine
Returns the view component related to the adapter
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
public __get (mixed $propertyName) inherited from Phalcon\Di\Injectable
Magic method __get
Class Phalcon\Mvc\View\Engine\Volt\Compiler¶
implements Phalcon\Di\InjectionAwareInterface
This class reads and compiles Volt templates into PHP plain code
<?php
$compiler = new \Phalcon\Mvc\View\Engine\Volt\Compiler();
$compiler->compile("views/partials/header.volt");
require $compiler->getCompiledTemplatePath();
Methods¶
public __construct ([Phalcon\Mvc\ViewBaseInterface $view])
public setDI (Phalcon\DiInterface $dependencyInjector)
Sets the dependency injector
public getDI ()
Returns the internal dependency injector
public setOptions (array $options)
Sets the compiler options
public setOption (string $option, mixed $value)
Sets a single compiler option
public string getOption (string $option)
Returns a compiler's option
public getOptions ()
Returns the compiler options
final public mixed fireExtensionEvent (string $name, [array $arguments])
Fires an event to registered extensions
public addExtension (mixed $extension)
Registers a Volt's extension
public getExtensions ()
Returns the list of extensions registered in Volt
public addFunction (mixed $name, mixed $definition)
Register a new function in the compiler
public getFunctions ()
Register the user registered functions
public addFilter (mixed $name, mixed $definition)
Register a new filter in the compiler
public getFilters ()
Register the user registered filters
public setUniquePrefix (mixed $prefix)
Set a unique prefix to be used as prefix for compiled variables
public getUniquePrefix ()
Return a unique prefix to be used as prefix for compiled variables and contexts
public attributeReader (array $expr)
Resolves attribute reading
public functionCall (array $expr)
Resolves function intermediate code into PHP function calls
public resolveTest (array $test, mixed $left)
Resolves filter intermediate code into a valid PHP expression
final protected resolveFilter (array $filter, mixed $left)
Resolves filter intermediate code into PHP function calls
final public expression (array $expr)
Resolves an expression node in an AST volt tree
final protected string | array _statementListOrExtends (array $statements)
Compiles a block of statements
public compileForeach (array $statement, [mixed $extendsMode])
Compiles a "foreach" intermediate code representation into plain PHP code
public compileForElse ()
Generates a 'forelse' PHP code
public compileIf (array $statement, [mixed $extendsMode])
Compiles a 'if' statement returning PHP code
public compileElseIf (array $statement)
Compiles a "elseif" statement returning PHP code
public compileCache (array $statement, [mixed $extendsMode])
Compiles a "cache" statement returning PHP code
public compileSet (array $statement)
Compiles a "set" statement returning PHP code
public compileDo (array $statement)
Compiles a "do" statement returning PHP code
public compileReturn (array $statement)
Compiles a "return" statement returning PHP code
public compileAutoEscape (array $statement, mixed $extendsMode)
Compiles a "autoescape" statement returning PHP code
public string compileEcho (array $statement)
Compiles a '{{' '}}' statement returning PHP code
public compileInclude (array $statement)
Compiles a 'include' statement returning PHP code
public compileMacro (array $statement, mixed $extendsMode)
Compiles macros
public string compileCall (array $statement, boolean $extendsMode)
Compiles calls to macros
final protected _statementList (array $statements, [mixed $extendsMode])
Traverses a statement list compiling each of its nodes
protected _compileSource (mixed $viewCode, [mixed $extendsMode])
Compiles a Volt source code returning a PHP plain version
public compileString (mixed $viewCode, [mixed $extendsMode])
Compiles a template into a string
public string | array compileFile (string $path, string $compiledPath, [boolean $extendsMode])
Compiles a template into a file forcing the destination path
public compile (mixed $templatePath, [mixed $extendsMode])
Compiles a template into a file applying the compiler options This method does not return the compiled path if the template was not compiled
public getTemplatePath ()
Returns the path that is currently being compiled
public getCompiledTemplatePath ()
Returns the path to the last compiled template
public array parse (string $viewCode)
Parses a Volt template returning its intermediate representation
protected getFinalPath (mixed $path)
Gets the final path with VIEW
Class Phalcon\Mvc\View\Engine\Volt\Exception¶
extends class Phalcon\Mvc\View\Exception
implements Throwable
Methods¶
final private Exception __clone () inherited from Exception
Clone the exception
public __construct ([mixed $message], [mixed $code], [mixed $previous]) inherited from Exception
Exception constructor
public __wakeup () inherited from Exception
...
final public string getMessage () inherited from Exception
Gets the Exception message
final public int getCode () inherited from Exception
Gets the Exception code
final public string getFile () inherited from Exception
Gets the file in which the exception occurred
final public int getLine () inherited from Exception
Gets the line in which the exception occurred
final public array getTrace () inherited from Exception
Gets the stack trace
final public Exception getPrevious () inherited from Exception
Returns previous Exception
final public Exception getTraceAsString () inherited from Exception
Gets the stack trace as a string
public string __toString () inherited from Exception
String representation of the exception
Interface Phalcon\Mvc\View\EngineInterface¶
Methods¶
abstract public getContent ()
...
abstract public partial (mixed $partialPath, [mixed $params])
...
abstract public render (mixed $path, mixed $params, [mixed $mustClean])
...
Class Phalcon\Mvc\View\Exception¶
extends class Phalcon\Exception
implements Throwable
Methods¶
final private Exception __clone () inherited from Exception
Clone the exception
public __construct ([mixed $message], [mixed $code], [mixed $previous]) inherited from Exception
Exception constructor
public __wakeup () inherited from Exception
...
final public string getMessage () inherited from Exception
Gets the Exception message
final public int getCode () inherited from Exception
Gets the Exception code
final public string getFile () inherited from Exception
Gets the file in which the exception occurred
final public int getLine () inherited from Exception
Gets the line in which the exception occurred
final public array getTrace () inherited from Exception
Gets the stack trace
final public Exception getPrevious () inherited from Exception
Returns previous Exception
final public Exception getTraceAsString () inherited from Exception
Gets the stack trace as a string
public string __toString () inherited from Exception
String representation of the exception
Class Phalcon\Mvc\View\Simple¶
extends abstract class Phalcon\Di\Injectable
implements Phalcon\Events\EventsAwareInterface, Phalcon\Di\InjectionAwareInterface, Phalcon\Mvc\ViewBaseInterface
This component allows to render views without hierarchical levels
<?php
use Phalcon\Mvc\View\Simple as View;
$view = new View();
// Render a view
echo $view->render(
"templates/my-view",
[
"some" => $param,
]
);
// Or with filename with extension
echo $view->render(
"templates/my-view.volt",
[
"parameter" => $here,
]
);
Methods¶
public getRegisteredEngines ()
public __construct ([array $options])
Phalcon\Mvc\View\Simple constructor
public setViewsDir (mixed $viewsDir)
Sets views directory. Depending of your platform, always add a trailing slash or backslash
public getViewsDir ()
Gets views directory
public registerEngines (array $engines)
Register templating engines
<?php
$this->view->registerEngines(
[
".phtml" => "Phalcon\Mvc\View\Engine\Php",
".volt" => "Phalcon\Mvc\View\Engine\Volt",
".mhtml" => "MyCustomEngine",
]
);
protected array _loadTemplateEngines ()
Loads registered template engines, if none is registered it will use Phalcon\Mvc\View\Engine\Php
final protected _internalRender (string $path, array $params)
Tries to render the view with every engine registered in the component
public render (string $path, [array $params])
Renders a view
public partial (mixed $partialPath, [mixed $params])
Renders a partial view
<?php
// Show a partial inside another view with parameters
$this->partial(
"shared/footer",
[
"content" => $html,
]
);
public setCacheOptions (array $options)
Sets the cache options
public array getCacheOptions ()
Returns the cache options
protected _createCache ()
Create a Phalcon\Cache based on the internal cache options
public getCache ()
Returns the cache instance used to cache
public cache ([mixed $options])
Cache the actual view render to certain level
public setParamToView (mixed $key, mixed $value)
Adds parameters to views (alias of setVar)
public setVars (array $params, [mixed $merge])
Set all the render params
public setVar (mixed $key, mixed $value)
Set a single view parameter
public getVar (mixed $key)
Returns a parameter previously set in the view
public array getParamsToView ()
Returns parameters to views
public setContent (mixed $content)
Externally sets the view content
public getContent ()
Returns cached output from another view stage
public string getActiveRenderPath ()
Returns the path of the view that is currently rendered
public __set (mixed $key, mixed $value)
Magic method to pass variables to the views
public __get (mixed $key)
Magic method to retrieve a variable passed to the view
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
Interface Phalcon\Mvc\ViewBaseInterface¶
Methods¶
abstract public setViewsDir (mixed $viewsDir)
...
abstract public getViewsDir ()
...
abstract public setParamToView (mixed $key, mixed $value)
...
abstract public setVar (mixed $key, mixed $value)
...
abstract public getParamsToView ()
...
abstract public getCache ()
...
abstract public cache ([mixed $options])
...
abstract public setContent (mixed $content)
...
abstract public getContent ()
...
abstract public partial (mixed $partialPath, [mixed $params])
...
Interface Phalcon\Mvc\ViewInterface¶
implements Phalcon\Mvc\ViewBaseInterface
Methods¶
abstract public setLayoutsDir (mixed $layoutsDir)
...
abstract public getLayoutsDir ()
...
abstract public setPartialsDir (mixed $partialsDir)
...
abstract public getPartialsDir ()
...
abstract public setBasePath (mixed $basePath)
...
abstract public getBasePath ()
...
abstract public setRenderLevel (mixed $level)
...
abstract public setMainView (mixed $viewPath)
...
abstract public getMainView ()
...
abstract public setLayout (mixed $layout)
...
abstract public getLayout ()
...
abstract public setTemplateBefore (mixed $templateBefore)
...
abstract public cleanTemplateBefore ()
...
abstract public setTemplateAfter (mixed $templateAfter)
...
abstract public cleanTemplateAfter ()
...
abstract public getControllerName ()
...
abstract public getActionName ()
...
abstract public getParams ()
...
abstract public start ()
...
abstract public registerEngines (array $engines)
...
abstract public render (mixed $controllerName, mixed $actionName, [mixed $params])
...
abstract public pick (mixed $renderView)
...
abstract public finish ()
...
abstract public getActiveRenderPath ()
...
abstract public disable ()
...
abstract public enable ()
...
abstract public reset ()
...
abstract public isDisabled ()
...
abstract public setViewsDir (mixed $viewsDir) inherited from Phalcon\Mvc\ViewBaseInterface
...
abstract public getViewsDir () inherited from Phalcon\Mvc\ViewBaseInterface
...
abstract public setParamToView (mixed $key, mixed $value) inherited from Phalcon\Mvc\ViewBaseInterface
...
abstract public setVar (mixed $key, mixed $value) inherited from Phalcon\Mvc\ViewBaseInterface
...
abstract public getParamsToView () inherited from Phalcon\Mvc\ViewBaseInterface
...
abstract public getCache () inherited from Phalcon\Mvc\ViewBaseInterface
...
abstract public cache ([mixed $options]) inherited from Phalcon\Mvc\ViewBaseInterface
...
abstract public setContent (mixed $content) inherited from Phalcon\Mvc\ViewBaseInterface
...
abstract public getContent () inherited from Phalcon\Mvc\ViewBaseInterface
...
abstract public partial (mixed $partialPath, [mixed $params]) inherited from Phalcon\Mvc\ViewBaseInterface
...