Documentation
Views & Templates
Blade Template

Blade template

As you maybe 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 (opens in a new tab).

Blade template files use the .blade.php file extension and are typically stored in the resources/views directory.

Basic Usage

The only things that 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 at 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()])
      ->withAdminStyles('prism')
      ->withAdminScripts('prism')
      ->withAdminStyles('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

BladeOne ogo

We're using the BladeOne (opens in a new tab) 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 template outside Laravel.

If you want learn more about Blade directives, please visit the BladeOne documentation (opens in a new tab) or the Laravel documentation (opens in a new tab).