Blog Detail

13

Jun
How to Use Laravel Blade Engine to Render Files From CLI cover image

arrow_back How to Use Laravel Blade Engine to Render Files From CLI

Blade CLI is a package that customizes and extends several of the Illuminate\View classes used by the blade engine to be able to use simple blade features/directives (i.e @if, @include, @foreach, etc.) on files. That said, the more advanced features of the engine are out of the scope of what this package was meant for and may not be supported, keep that in mind if you are attempting more advanced usages.

Installation

Download specific tag version release from releases and make available in $PATH:

# in ~/.bashrc or equivalent
PATH=/usr/local/bin/blade-cli:$PATH

Install dependencies:

composer install

Confirm is executable:

blade

Or if you want to use the API directly as a package, you can install it with composer:

composer require surgiie/blade-cli

and use the class directly

use BladeCLI\Blade;
use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;

$blade = new Blade(
    container: new Container,
    filesystem: new Filesystem,
    filePath: '/path/to/file/to/render',
    options: [
        'force'=> true, // force overwrite existing rendered file
        'save-directory'=>'save-to-dir' // optional directory to save rendered file to. Default is current directory.
    ]
);

// render the file with this data/vars
$blade->render([
    'var'=>'example'
]);

CLI Completion

You may optionally source the provided completion script for bash completion:

source /usr/local/bin/blade-cli/completion

Usage

Let’s work through an example, given this file exists in your current directory (person.yml):

name: {{ $name }}
relationship: {{ $relationship }}
favorite_food: {{ $favoriteFood }}
@if($includeAddress)
address: "123 example lane"
@endif

You may render that file as follows:

blade render ./person.yml \
                --name="Bob"
                --relationship="Uncle"
                --favorite-food="Pizza"
                --include-address

This will render and save the file to the same directory as person.rendered.yml

Custom Save Directory

All files will get saved to the current directory as < filename >.rendered.< extension > or simply < filename >.rendered if the file does not have an extension when you do not provide a custom directory to save the rendered file to. This is to avoid overwriting the file you are rendering. If you wish to save to a custom directory use the –save-directory option to specify a directory to write the file to:

php blade render ./person.yml \
                --name="Bob"
                --relationship="Uncle"
                --favorite-food="Pizza"
                --include-address
                --save-directory="rendered-files/"

The blade class will attempt to automatically ensure the directory exists if it can write to it. In the above example, the result of ./person.yml would get written to ./rendered-files/person.yml.

This package has a lot more features and options. If you want to dig more about this package, you can visit its complete documentation on Github

Published at : 13-06-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project