Laravel
Here is a sample code to render the content of the block inside Laravel. Every page component is rendered with a view named after the identifier with passing the data as variables with an additional fields
variable containing all the fields.
class BlockRenderer
{
/*
* Interceptors acts as middlewares
* and can transform the data with
* the inject(string $blocName, array $data) method
*/
private static $interceptors = [
'my-block' => MyBlockInterceptor::class,
];
public static function render($blocs): string
{
if (is_string($blocs)) {
$blocs = json_decode($blocs, true);
}
return collect($blocs)->map(function ($bloc) {
return self::renderBlock($bloc);
})->join('');
}
private static function renderBlock(array $data): string
{
$request = request();
$blocName = $data['_name'];
$data['isPreview'] = false;
if (isset(self::$interceptors[$blocName])) {
$data = app(self::$interceptors[$blocName])->inject($blocName, $data);
}
if ($request->route()->getName() === 'preview') {
$data['isPreview'] = true;
}
return view("front.blocs.{$blocName}", array_merge(['fields' => $data], $data))->render();
}
}