-
Notifications
You must be signed in to change notification settings - Fork 0
Components
This files are only used with Twig Template Engine because they are an extension of it. The aim of them is to ease development process and create reusable code. They are under the app/components part and the folders are called plugin inside the folders called components of that plugin.
For example: We have a Blog plugin and it has various components such as Posts, Post...
Components has certain variables and functions to function. Every component starts with __construct, that is class constructor in PHP. If you want to export some variables into the template component scope, you can use exports variable array given in the class. The exports will be removed with the scope of the place in the template.
There are some types of these components BasicComponent, ConditionComponent and RepeatComponent.
This is the simplest component and its only function is to display. Some actions can be done inside the component and then reflected to the template. It is defined as:
<?php
Filename: app/components/Blog/Post.php
use Just\Component\BasicComponent;
class Post extends BasicComponent{
public function __construct(){
//..
}
}
And used in template as:
{% component Blog.Post %}
<title>Some html code</title>
{% endcomponent %}
If some parameters should be taken from the template its done by with tag. Each parameter are separated by comma and they are send to the __construct function. Here is an example of 3 parameters given to the controller
{% component Blog.Post with user, foo, bar %}
...
{% endcomponent %}
The component class should be like this:
<?php
Filename: app/components/Blog/Post.php
use Just\Component\BasicComponent;
class Post extends BasicComponent{
public function __construct($user, $foo, $bar){
//..
}
}
This is a component type that performs if given condition is correct. For example, if your user is logged in you want to show the data otherwise, just don't render it. This is done by condition variable registered in the class. It has all the functionality that BasicComponent has like exports.
Component Code:
<?php
//Filename: app/components/User/Info.php
use Just\Component\ConditionComponent;
//Some custom Auth class (not important)
use App\Ext\Auth;
class Info extends ConditionComponent{
public function __construct(){
//The stuff that checks whether user is logged in
$this->condition = Auth::check()
}
}
Template Code:
{% component User.Info %}
Hello My Friend
{% endcomponent %}
This is a component that takes a sequence and repeats it until the sequence is end up. The sequence variable is to hold that sequence to use in template. You should assign your stuff in that. The key and value is default iteration unit names for the template scope. If you want to change them just assign key and value variables the names you want as string values.
<?php
use Just\Component\RepeatComponent;
class Posts extends RepeatComponent{
//These are optional but useful, If you left them blank they will be "key" and "value"
public $key = "count";
public $value = "post";
public function __construct(){
//The data sequence coming from somewhere like db is assigned to the sequence.
$this->sequence = ...
}
}
Example template code:
<ul>
{% component Blog.Posts %}
<li>
{{post.title}} - {{post.text}}
</li>
{% endcomponent %}
</ul>