Documentation

View
in package

Template processor

This class is used for server side HTML rendering with the help of PHP.

Table of Contents

CONFIG_DEFAULT_FOLDER  = 'view.default_folder'
$blockQueue  : array<string|int, mixed>
Holds the blocks queue for start/end block functions
$blocks  : array<string|int, mixed>
Holds the content of the blocks by name
$config  : Config
$data  : array<string|int, mixed>
Holds the view variables
$folders  : array<string|int, mixed>
Stores the paths for the folders by namespace
$functionsIncluded  : bool
The functions were included?
$layout  : string
The layout for the currently fetched template
$router  : Router
$scripts  : array<string|int, mixed>
Holds the scripts for the view
$styles  : array<string|int, mixed>
Holds the styles for the view
$theme  : string
The theme path for overriding templates
__construct()  : mixed
addFolder()  : void
Adds a view folder
addScript()  : void
Adds a script for the view
addStyle()  : void
Adds a style for the view
block()  : string
Returns with the content of a block
endBlock()  : void
Ends a block
fetch()  : string
Fetches a template with variables
folder()  : string
Returns with the folder path by namespace
get()  : mixed|null
Returns with a view variable value by name
layout()  : string
Returns with the current layout
scripts()  : array<string|int, mixed>
Returns with the scripts in [src => [attributes]] array
set()  : void
Sets a view variable
setTheme()  : void
Sets the theme path for overriding templates
startBlock()  : void
Starts a block
styles()  : array<string|int, mixed>
Returns with the styles in [src => [attributes]] array
theme()  : string
Returns with the theme path
useLayout()  : void
Sets the `$layout` so after the template was rendered it will render this layout.
getRealPath()  : string
Returns with the real path to the template file
includeFunctions()  : mixed
Includes the functions.php of the theme, of the app and of the framework

Constants

CONFIG_DEFAULT_FOLDER

public mixed CONFIG_DEFAULT_FOLDER = 'view.default_folder'

Properties

$blockQueue

Holds the blocks queue for start/end block functions

protected array<string|int, mixed> $blockQueue = []

$blocks

Holds the content of the blocks by name

protected array<string|int, mixed> $blocks = []

$data

Holds the view variables

protected array<string|int, mixed> $data = []

$folders

Stores the paths for the folders by namespace

protected array<string|int, mixed> $folders = []

$functionsIncluded

The functions were included?

protected bool $functionsIncluded = false

$layout

The layout for the currently fetched template

protected string $layout = ''

$scripts

Holds the scripts for the view

protected array<string|int, mixed> $scripts = []

$styles

Holds the styles for the view

protected array<string|int, mixed> $styles = []

$theme

The theme path for overriding templates

protected string $theme = ''

Methods

__construct()

public __construct(Config $config) : mixed
Parameters
$config : Config
Return values
mixed

addFolder()

Adds a view folder

public addFolder(string $namespace, string $path) : void

For example, if the app.root_path config value is '/var/www/example.com', and you have the following code:

$view->addFolder('folder', '~/views/example');
$view->fetch('folder:index');

will fetch the '/var/www/example.com/views/example/index.phtml'.

The $path should NOT end with a '/'

Parameters
$namespace : string

The namespace of the folder

$path : string

The path to the folder. It can be an absolute or relative path as well.

Return values
void

addScript()

Adds a script for the view

public addScript(string $src[, array<string|int, mixed> $attributes = [] ]) : void
Parameters
$src : string
$attributes : array<string|int, mixed> = []
Return values
void

addStyle()

Adds a style for the view

public addStyle(string $src[, array<string|int, mixed> $attributes = [] ]) : void
Parameters
$src : string
$attributes : array<string|int, mixed> = []
Return values
void

block()

Returns with the content of a block

public block(string $name) : string
Parameters
$name : string
Return values
string

endBlock()

Ends a block

public endBlock() : void

If the block has content, the new content will be appended to the block.

Return values
void

fetch()

Fetches a template with variables

public fetch(string $__viewPath[, array<string|int, mixed> $__vars = [] ]) : string
Parameters
$__viewPath : string

The path to the view

$__vars : array<string|int, mixed> = []

The variables in [name => value] format

Return values
string

The fetched template output in string

folder()

Returns with the folder path by namespace

public folder(string $namespace) : string
Parameters
$namespace : string
Return values
string

The path for the folder

get()

Returns with a view variable value by name

public get(string $name[, mixed|null $default = null ]) : mixed|null
Parameters
$name : string

The name of the variable

$default : mixed|null = null

The default value if the variable doesn't present

Return values
mixed|null

The value of the variable

layout()

Returns with the current layout

public layout() : string
Return values
string

The current layout

scripts()

Returns with the scripts in [src => [attributes]] array

public scripts() : array<string|int, mixed>
Return values
array<string|int, mixed>

The scripts array

set()

Sets a view variable

public set(string $name, mixed $value) : void
Parameters
$name : string

The name of the variable

$value : mixed

The value of the variable

Return values
void

setTheme()

Sets the theme path for overriding templates

public setTheme(string $path) : void
Parameters
$path : string
Return values
void

startBlock()

Starts a block

public startBlock(string $name) : void

If the block has content, when the endBlock() will be called the content will be appended to the block.

Parameters
$name : string

The name of the block

Return values
void

styles()

Returns with the styles in [src => [attributes]] array

public styles() : array<string|int, mixed>
Return values
array<string|int, mixed>

The styles array

theme()

Returns with the theme path

public theme() : string
Return values
string

useLayout()

Sets the `$layout` so after the template was rendered it will render this layout.

public useLayout(string $path) : void

The path can contain a namespace if a folder was added with that namespace, for example:

$view->addFolder('folder', 'views/example');
$view->useLayout('folder:layout');

will render the 'views/example/layout.phtml'.

Parameters
$path : string

The path for the layout, for example: 'layout' is for 'views/layout.phtml'

Return values
void

getRealPath()

Returns with the real path to the template file

protected getRealPath(string $path) : string
  • If the path doesn't contain a namespace it will use the view.default_folder config value to determine the path for the folder.
  • If the path contains a namespace it will use the folder of the namespace.
  • If the view has a theme, that path will be checked first, so the theme can override every template.

For example if you added a folder with namespace 'folder':

$view->addFolder('folder', '~/views/example');

and the app.root_path config value is '/var/www/example.com' the result will be '/var/www/example.com/views/example/index.phtml'

Parameters
$path : string

The view path

Tags
throws
MicroException

If the view path has a namespace but a folder wasn't added for it

Return values
string

The real path to the template file

includeFunctions()

Includes the functions.php of the theme, of the app and of the framework

protected includeFunctions() : mixed
Return values
mixed

Search results