Using Blade outside Laravel

Sun 12th Aug 2018 / Last Updated: Sat 23rd Nov 2019 / Tags: #Laravel, #PHP

Using Jens Segers' Blade package to render Blade templates in a standalone PHP application.

(I actually use this to render this blog.)

Table of Contents

  1. Installation
  2. Usage

Installation

composer require jenssegers/blade

Usage

use Jenssegers\Blade\Blade;

// Settings
$viewPath  = __DIR__ . '/views';
$cachePath = __DIR__ . '/tmp/view-cache';

// Create cache directory (or do this manually)
if (!@mkdir($cachePath, 0755, true) && !is_dir($cachePath)) {
    throw new RuntimeException("Directory '$cachePath' could not be created");
}

// Execute
$view = 'pages.index';
$data = ['pages' => $pages];

$blade = new Blade($viewPath, $cachePath);
$html = $blade->render($view, $data);

// Display
echo $html;