Download the PHP package micky/mkyengine without Composer
On this page you can find all versions of the php package micky/mkyengine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mkyengine
MkyEngine
The template engine uses the block and extension system to define the different parts of the view.
Installation
composer require micky/mkyengine
Usage
Directory Loader
The directory loader register view directory for template engine
If you want to define component or layout subdirectory use setComponentDir or setLayoutDir
component directory will be views_dir/components_dir
and layout directory will be views_dir/layouts_dir
Environment
The environment stores all directories with the namespace and file extension of the view, you can optionally define shared variables
By default, the first namespace is root, you can add another directory loader and its namespace with the method addLoader()
To use view, component or layout from another namespace use @namespace:view
View Compiler
The view compiler compiles the view by retrieving the blocks and displaying them in the layout sections. The first parameter is the environment, the second is the view to be displayed and the third is the view variables.
To render the view use render()
method
Templating
Layout
The layout is a background for the views, you can define which parts of the layout will be filled by the view.
You can define a default value in the section that will be used if no 'title' block is defined.
Layout view can extend another layout.
Block
Extends
Extends method is used to define the layout file.
Blocks are a part of view use for layout. The param is the block name
You can set a simple block with a second parameter
To display this block use section() method in the layout with the block name to display
You can define several blocks with the same name
It will show
Thanks to that, blocks can be conditioned by the method if()
Injection
Thanks to injection method $this->inject
you can set object used for HTML template like number formatter or form builder as View property
Example FormBuilder:
In the view:
The first parameter is the name of property you want to register in the view instance and the second is the class to instantiate, you can pass a class instance or a class name.
You will be able to use class via $this->nameOfProperty
The HTML rendering will be:
Component
The component is a view piece, useful if you want to use it in several views.
You can pass a variable to the component with the method bind()
, the first parameter is the component variable and the second is the value.
You can pass multiple variables to the component with the method binds()
.
Same as block class, components can be conditioned by the method if()
You can repeat a component in loop with 2 methods:
- for
- each
For loop
The first parameter is the number of iterations, the second is a callback that will be called at each iteration. In callback the first parameter is the view params, the second is the current loop index.
Example: in name-input.php component there a variable called 'name' for an input, with the callback each iterated component will have the name of the current user
Each
With the method each()
, the component will iterate for each array value. The first parameter is the array and the second can be a callback, an array or a string.
Callback
The each()
callback is the same as the for()
callback but with a third parameter that it's the array.
Array
You can bind variable with an array that index is the component variable and the value is the object property or array key of data $users
If you need to pass a nested value, you can do so by concatenating address.postcode
it's equal to:
$user->{address | getAddress() | magic getter for "address"}->{postcode | getPostcode() | magic getter for "postcode"}
Example:
$user->address->postcode
$user->getAddress()->postcode
$user->address['postcode']
$user->getAddress()['postcode']
$user['address']->postcode
$user['address']['postcode']
All properties and nested properties are accessible if the property exists or a getter, like for postcode
, getPostcode()
exists or a magic method exists
String
The component may need the object or array as parameter (like one user of users), for that you can set in the parameter the name of current iterated data
Else component
If the data $users
is empty you can set a third parameter as string to define the else component
Component slot
In the case you have a component which you want to make the body dynamic like:
A simple alert component with a conditional button, to use this in your view, you have to set three slots: the default
, confirm
and close
slot
The HTML rendering will be:
All texts not in a slot will be placed in the default slot. Slots can be conditional:
You can also make a default value for empty slot to avoid error message
Licence
MIT