Blade
Blade is a templating engine used primarily with the Laravel framework. It enables developers to create expressive, clean templates using a syntax that extends PHP in a simple and intuitive way. Blade templates facilitate common tasks like data display and layout management, helping streamline the development of dynamic web pages.
Setup
To use Blade templates with Blockstudio, you first need to install the jenssegers/blade package using Composer.
composer require jenssegers/bladeNext, you need to add a filter to your theme's functions.php file to tell Blockstudio how to render Blade templates. Blockstudio automatically collects all Blade templates and maps their paths, allowing you to use Blade's dot syntax for includes and layouts.
add_filter(
"blockstudio/blocks/render",
function ($value, $block) {
$blockPath = $block->path;
if (str_ends_with($blockPath, ".blade.php")) {
// Ensure the Blade class exists before trying to use it.
if (!class_exists('Jenssegers\Blade\Blade')) {
// Optionally, you could log an error or return a message.
return 'Error: Blade class not found. Please run "composer require jenssegers/blade".';
}
$data = $block->blockstudio["data"];
$bladeData = $data["blade"];
$blade = new \Jenssegers\Blade\Blade($bladeData["path"], sys_get_temp_dir());
return $blade->render($bladeData["templates"][$block->name], [
"a" => $data["attributes"],
"attributes" => $data["attributes"],
"b" => $data["block"],
"block" => $data["block"],
"c" => $data["context"],
"context" => $data["context"],
]);
}
return $value;
},
10,
2
);Template Variables
Your Blade templates will have access to the following variables:
| Variable | Description |
|---|---|
$a | An alias for $attributes |
$attributes | The block's attributes |
$b | An alias for $block |
$block | Data related to the block itself |
$c | An alias for $context |
$context | The block's context (e.g., post ID, post type when in a loop) |
Usage
To use Blade for your block's template, create an index.blade.php file in your block's directory. Blockstudio will then automatically use this file for rendering the block.
<div class="my-block">
@if($a['title'])
<h2>{{ $a['title'] }}</h2>
@endif
@if($a['content'])
<p>{{ $a['content'] }}</p>
@endif
</div>