Yet another template engine for PHP
This project is maintained by Corviz
Namespaces MUST begin and end with \
Example:
$componentsNamespace = '\\App\\View\\Components\\';
Crow::setComponentsNamespace($componentsNamespace);
namespace App\View\Components;
use Corviz\Crow\Component;
class AlertComponent extends Component
{
/**
* Draw component
*
* @return void
*/
public function render(): void
{
$this->view('components/alert');
}
}
IMPORTANT! All components classes MUST end with the word “Component” (BradcrumbsComponent, AlertComponent, SlideshowComponent and so on…)
alert.crow.php
<div class="alert alert-{{ $attributes['level'] }}">
{!! $contents !!}
</div>
Component tags are preceded with “x-“. It uses dash case (Eg.:
{{-- Template contents... --}}
<x-alert level="info">
You have a new message!
</x-alert>
{{-- More contents... --}}
This will produce the following html:
<div class="alert alert-info">
You have a new message!
</div>
$attributes and $contents are fed by default to you component template. They contain the attribute values in an array and the component’s body contents respectively.
It is also possible to read this data through getContents()
and getAttributes()
methods inside your component class.
public function render(): void
{
$attrs = $this->getAttributes();
$contents = $this->getContents();
//do something...
$this->view('components/display-numbers');
}
public function render(): void
{
$sum = 1 + 1;
$sub = 10 - 1;
$this->view('components/display-numbers', compact('sum', 'sub'));
}
<x-alert :level="$level">
Message
</x-alert>
or
<x-alert :level="$message->type == 'error' ? 'danger' : 'info'">
{{ $alertMessage }}
</x-alert>
<x-my-component something="..." />
Enjoy!