Skip to content

Instantly share code, notes, and snippets.

@chwnam
Created August 6, 2020 07:23
Show Gist options
  • Save chwnam/9dbe33036c7b037d6c0992293ad63ab4 to your computer and use it in GitHub Desktop.
Save chwnam/9dbe33036c7b037d6c0992293ad63ab4 to your computer and use it in GitHub Desktop.
Using Blade template engine in WordPress plugins.
<?php
/**
* Plugin Name: Blade Test
*/
/*
# composer.json
{
"name": "naran/experimental",
"autoload": {
"psr-4": {
"Naran\\Experimental\\": "src/"
}
},
"require": {
"illuminate/container": "^7.23",
"illuminate/view": "^7.23",
"illuminate/config": "^7.23"
},
"require-dev": {}
}
# src/MyRepository.php
namespace Naran\Experimental;
use Illuminate\Config\Repository;
class MyRepository extends Repository
{
}
*/
if ( ! file_exists(__DIR__ . '/vendor/autoload.php')) {
return;
}
require_once __DIR__ . '/vendor/autoload.php';
add_action('template_redirect', 'naran_experimental_template');
function naran_experimental_template()
{
if (is_page('sample-page')) {
$container = \Illuminate\Container\Container::getInstance();
$container->bindIf('config', \Naran\Experimental\MyRepository::class, true);
$container->bindIf('files', \Illuminate\Filesystem\Filesystem::class, true);
$container->bindIf('events', \Illuminate\Events\Dispatcher::class, true);
$config = $container->make('config');
$config->set(
[
'view.compiled' => __DIR__ . '/templates-complied',
'view.paths' => [
get_stylesheet_directory() . '/naran-experimental/templates',
get_template_directory() . '/naran-experimental/templates',
__DIR__ . '/templates',
]
]
);
(new \Illuminate\View\ViewServiceProvider($container))->register();
$output = $container->make('view')
->make('greetings', ['user_name' => 'Cristy'])
->render();
echo $output;
$output = $container->make('view')
->file(__DIR__ . '/templates/favorites.blade.php', ['item' => 'donut'])
->render();
echo $output;
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment