fiskhandlarn / blade

:runner: A library for using Laravel Blade templates in WordPlate.
GNU General Public License v3.0
37 stars 5 forks source link
blade blade-template laravel render template view wordplate wordpress wordpress-php-library

blade

blade

A library for using Laravel Blade templates in WordPress/WordPlate.

Build Status Coverage Status Total Downloads Latest Version License

Installation

Require this package, with Composer, in the root directory of your project.

$ composer require fiskhandlarn/blade

Usage

Render

Use helper function blade:

blade('index', ['machine' => 'Voight-Kampff']);

(This renders and echoes the template /resources/views/index.blade.php and caches it to /storage/views.)

... or instantiate Blade by passing the folder(s) where your view files are located, and a cache folder. Render a template by calling the render method.

use Fiskhandlarn\Blade;

$blade = new Blade(get_stylesheet_directory() . '/views', get_stylesheet_directory() . '/cache');

echo $blade->render('index', ['machine' => 'Voight-Kampff']);

Render with data from a controller class

Use helper function blade_controller:

blade_controller('index', 'Index');

(This renders and echoes the template /resources/views/index.blade.php with data generated from App\Controllers\Index.)

Controller classes must extend Fiskhandlarn\BladeController.

... or use the renderController method on a Blade object:

echo $blade->renderController('index', 'Index');

You can also pass additional data (this won't override properties from the controller though):

blade_controller('index', 'Index', ['lifespan' => "A coding sequence cannot be revised once it's been established."]);
echo $blade->renderController('index', 'Index', ['lifespan' => "A coding sequence cannot be revised once it's been established."]);

See soberwp/controller for more info on how to use controllers.

Supported features:

Unsupported features:

Untested features:

Unnecessary features:

Custom directive

Create a custom directive with helper function blade_directive:

blade_directive('datetime', function ($expression) {
    return "<?php echo with({$expression})->format('Y-m-d H:i:s'); ?>";
});

... or use the directive method on a Blade object:

$blade->directive('datetime', function ($expression) {
    return "<?php echo with({$expression})->format('Y-m-d H:i:s'); ?>";
});

Then you can use the directive in your templates:

{{-- In your Blade template --}}
@php $dateObj = new DateTime('2019-11-01 00:02:42') @endphp
@datetime($dateObj)

Custom composer

Create a custom composer with helper function blade_composer:

// Make variable available in all views
blade_composer('*', function ($view) {
    $view->with(['badge' => 'B26354']);
});

... or use the composer method on a Blade object:

// Make variable available in all views
$blade->composer('*', function ($view) {
    $view->with(['badge' => 'B26354']);
});

Share variables

Share variables across all templates with helper function blade_share:

// Make variable available in all views
blade_share(['badge' => 'B26354']);

... or use the share method on a Blade object:

$blade->share(['badge' => 'B26354']);

Extension

The Blade class passes all method calls to the internal compiler (see documentation) or view factory (see documentation for info on exists, first and creator).

Cache

If WP_DEBUG is set to true templates will always be rendered and updated.

Multisite

If run on a WordPress Multisite the cached files will be separated in subfolders by each site's blog id.

Filters

Use the blade/view/paths filter to customize the base paths where your templates are stored. (Default value is /resources/views.)

add_filter('blade/view/paths', function ($paths) {
    $paths = (array) $paths;

    $paths[] = get_stylesheet_directory() . '/views';

    return $paths;
});

Use the blade/cache/path filter to customize the cache folder path. (Default value is /storage/views.)

add_filter('blade/cache/path', function ($path) {
    $uploadDir = wp_upload_dir();
    return $uploadDir['basedir'] . '/.bladecache/';
});

Use the blade/cache/path filter to control creation of cache folder. (Default value is true.)

add_filter('blade/cache/create', '__return_false');

Use the blade/controller/namespace filter to customize the controller namespace. (Default value is App\Controllers.)

add_filter('blade/controller/namespace', function () {
    return 'MyApp\Controllers';
});

License

GNU GPL 3.0+