Download the PHP package devanych/view-renderer without Composer
On this page you can find all versions of the php package devanych/view-renderer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download devanych/view-renderer
More information about devanych/view-renderer
Files in devanych/view-renderer
Informations about the package view-renderer
Simple View Renderer
A small and easy-to-use PHP library designed for rendering native PHP views.
Highlights:
- Convenient flat block system.
- Setting global parameters for all views.
- Support for using existing PHP functions.
- Easy extensibility by adding custom functions.
- Flexible layout functionality with unlimited nesting.
A guide with a detailed description in Russian language is available here.
Installation
This package requires PHP version 7.4 or later.
Usage
The simplest example of rendering:
The render()
method takes as its first argument the path to the view file, which must be relative to the directory passed to the constructor when the object was created. The second argument passes an array of parameters (name => value
) that will be converted to variables inside the view.
in the view:
The view file can be with or without an extension, if the file extension is not specified, .php
will be substituted by default. The default extension can be changed by passing it to the constructor as the second argument.
Within layouts and views, a renderer instance is available at $this
.
Blocks
The block()
, beginBlock()
, and endBlock()
methods allow you to create content blocks in your view. The content of blocks is saved for use anywhere in the view, as well as in any parent layout. The method renderBlock()
renders the stored content of the block.
Layout code:
View code:
Rendering result:
Blocks can't be nested, but you can render one block inside another.
Note that content
is a reserved block name. it renders the content of the view and child layouts. If you try to create a block named content
, the \RuntimeException
exception is thrown.
When calling the renderBlock()
method with the name of a nonexistent block, the renderBlock()
method returns an empty string, so no additional methods are needed to check the existence of the block. The second argument in renderBlock()
can pass the default value, which will be substituted if the block with the specified name does not exist.
Layouts
Layouts are a special type of representations that are very convenient to use for rendering repetitive parts (header, footer, sidebar, etc.). This package allows you to build layout chains with unlimited nesting. Layout inheritance is set in the file of the view or child layout by the layout()
method.
Parent layout code (layouts/main
):
Child layout code (layouts/columns
):
View code (site/page
):
Rendering the view:
Rendering result:
If you just need to render the child layout file from the parent layout, you can use the render()
method.
Rendering result:
Extensions
The extension functionality allows you to add your own functions and use them inside layouts and views. To add a function, you need to create your own class that implements the Devanych\View\Extension\ExtensionInterface interface. The interface contains only one method that returns an array whose keys are the names of added functions, and the values can be any possible variants of the callable
type.
This package contains a single extension Devanych\View\Extension\AssetExtension, which simplifies the connection of assets (scripts, styles, fonts, etc.) and adds a timestamp to the file. To add an extension, use the addExtension()
method.
Global parameters
Using the addGlobal()
method, you can add global parameters that will be available in all views and layouts.
Adding a variable with the same name again will cause a \RuntimeException
, but you can change the value of the global variable for a specific view by passing a parameter with the same name when rendering the view, or simply assigning it in the view itself.
Escaping
To prevent the output of potentially dangerous HTML code, the renderer contains the esc()
method, which escapes special characters, converts them to the corresponding HTML entities.
This method is for convenience, but you can also use the original htmlspecialchars()
function, as all existing PHP functions are available in views and layouts.