Blade Template
As you may already know, the Blade template engine is used to generate HTML. The Blade template engine is a simple yet powerful templating language that is used by the Laravel framework. For more information on the Blade template engine, please visit the Laravel documentation.
Blade template files use the .blade.php
file extension and are typically stored in the resources/views
directory.
Basic Usage
The only thing you have to do to use Blade templates in your plugin is to create a view file in the resources/views
directory. For example, you can create a file named dashboard.blade.php
in the resources/views
directory.
You will be able to use the Blade template in the same way as you would use the current PHP template. For example, a typical controller method to render a Blade template in resources/views/blade/demo.blade.php
would look like this:
<?php
namespace WPKirk\Http\Controllers;
use WPKirk\Http\Controllers\EloquentUser as User;
class ExampleBladeController extends Controller
{
public function index()
{
return WPKirk()
->view('blade.demo', ['users' => User::all()])
->withAdminStyle('prism')
->withAdminScript('prism')
->withAdminStyle('wp-kirk-common');
}
}
In the example above, we are using the WPKirk()->view()
method to render the view. The WPKirk()->view()
method accepts the name of the view and the data that you want to pass to the view. As you can see, it works like the WPKirk()->view()
method of the current PHP template.
Of course, you may also pass the data to the view using the WPKirk()->view()
method as a second parameter.
In the resources/views/blade/demo.blade.php
file, you can use the data passed to the view like this:
<div class="wp-kirk wrap wp-kirk-sample">
<h1>Demo Blade</h1>
@foreach ($users as $user)
<p>user_nicename: {{ $user->user_nicename }}</p>
<p>user_email: {{ $user->user_email }}</p>
@endforeach
</div>
BladeOne Composer Package
We’re using the BladeOne template engine. BladeOne is a standalone version of Blade Template Engine that uses a single PHP file and can be ported and used in different projects. It allows you to use Blade templates outside Laravel.
If you want to learn more about Blade directives, please visit the BladeOne documentation or the Laravel documentation.