Render files from the command line with Blade
Published on by Paul Redmond
With the Laravel Blade CLI package, you can use the Blade engine to render files from the command line. This package doesn't support 100% of all Blade directives you might be familiar with in Laravel but does support common ones like @if
, @else
, @foreach
, @forelse
, @while
, etc.
Give the following example file from the readme:
name: {{ $name }}relationship: {{ $relationship }}favorite_food: {{ $favoriteFood }}@if($includeAddress)address: "123 example lane"@endif
You can render that file using the CLI:
blade render ./person.yml \ --name="Bob" \ --relationship="Uncle" \ --favorite-food="Pizza" \ --include-address \ --save-directory="build/"
Which will save the file at ./build/person.yml
Installing this project globally via composer gives you access to the blade
CLI command. However, you can also use the code directly from this package:
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']);
You can also pass data to the template via JSON using the --from-json
flag, which accepts a path to a JSON file. Finally, you can use this package to process an entire directory of templates:
php blade render templates/ --some-data=foo --force
You can learn about this package, get full installation instructions, and view the source code on GitHub.