Use Laravel's blade engine as a CLI for rendering files.

Overview

Blade CLI

tests

Use Laravel's blade engine as a CLI for rendering files.

Introduction

This package 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 scope of what this package was meant for and may not be supported.

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 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 the directory the file is in.
    ]
);

// 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

Use

Lets 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 same directory as the file being rendered 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 rendred 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 the result of ./person.yml would get written to ./rendered-files/person.yml.

Variable Data

There are 2 options for passing variable data to your files being rendered:

  1. As you saw in the earlier example above, the first method is through options to the render command. --example-var=value

  2. Using json files via the --from-json option to pass a path to a json file. This maybe passed multiple times to load from many files. Note that options take precedence over the data loaded from json files.

Variable Naming Convention

Options or keys in your json file can be defined in any naming convention you prefer, but your actual variable reference should be camel case. This is because php doesnt support kebab cased variables which is often the format for command line options. That said, since camel case is usually standard, that is the format we decided to stick with. Your options will automatically get converted to data using camel case. To clarify a bit:

Either one of these option formats can be used --favorite-food, --favoriteFood, --favorite_food to reference a {{ $favoriteFood }} variable in your file.

Variable Types

These are the current supported way to pass variables for different types/purposes:

String/Single Value Variables

Use simple option key/value format for passing variables for single/string values:

--foo=bar --bar=baz

Array Value Variables

For array variables, just pass the option more than once:

--names=Steve --names=Ricky --names=Bob

True Boolean Value Variables

For boolean true variables, just pass the option with no value:

--should-do-thing

Note Since variable options are dynamic the "negate/false" options are not supported. Instead do something like this in your files {{ $shouldDoSomething ?? false }} to default to false and then use true options to "negate" the value.

Force write

If you try to render a file that already exists an exception will be raised, you may consider force write via the --force flag.

php blade render ./person.yml \
                --name="Bob"
                --relationship="Uncle"
                --favorite-food="Pizza"
                --include-address
                --force # force overwrite person.rendered.yml if it already exists.

Processing an entire directory of files

You may also pass the path to a directory instead of a single file. This might be useful if you like to group template files in a directory and

want to render them all with a single command:

php blade render templates/ --some-data=foo

Note This will prompt you for confirmation.

Force process directory

You may skip confirmation of rendering a directory's files with the --force flag:

php blade render templates/ --some-data=foo --force

Custom Directory for directory files:

By default, files will get saved to the same directory the file being rendered is in, as seen earlier, you may specify a custom directory to save rendered files in with the same --save-directory option:

php blade render templates/ --some-data=foo --save-directory="/home/bob/templates/"

Note When using a custom directory to save to, the directory specified will have files saved to mirror the directory being processed. In this example /home/bob/templates/ will have a directory structure that matches templates/.

Unit Testing

If utilizing the \BladeCLI\Blade class directly in an app, the following methods maybe utilized to make unit testing easier:

<?php

// turns on testing mode and will write files into the given testing directory.
Blade::fake('./testing-directory');

// write ./testing-directory/example.yaml to test render call on
Blade::putTestFile('example.yaml', 
<<<EOL
name: {{ \$name }}
favorite_food: {{ \$favoriteFood }}
pets:
    @foreach(\$dogs as \$dog)
    - {{ \$dog }}
    @endforeach
contact_info:
    phone: 1234567890
    @if(\$includeAddress)
    street_info: 123 Lane.
    @endif
EOL
);

// generates a path to the testing directory, ie ./testing-directory/example.yaml
Blade::testPath('example.yaml');

// asserts that file exists in ./testing-directory/example.rendered.yaml
Blade::assertRendered('example.rendered.yaml');

// assert the rendered file exists and matches the expected content
Blade::assertRendered('example.rendered.yaml', 
<<<EOL
name: Bob
favorite_food: Pizza
pets:
    - Rex
    - Charlie
contact_info:
    phone: 1234567890
    street_info: 123 Lane.
EOL);

// removes current testing directory and turns off testing mode
Blade::tearDown();
You might also like...
Blade is a simple, yet powerful templating engine provided for the Slim Framework

slim-blade Blade is the default template engine of Laravel. The main advantage of Blade is template inheritance whilst using plain PHP. This package a

A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

Install Install via composer. $ composer require olliecodes/laravel-etched-blade Once installed you'll want to publish the config. $ php artisan vendo

A package to easily make use of Iconic icons in your Laravel Blade views.
A package to easily make use of Iconic icons in your Laravel Blade views.

Blade Iconic A package to easily make use of Iconic icons in your Laravel Blade views. For a full list of available icons see the SVG directory. Iconi

A package to easily make use of Simple Icons in your Laravel Blade views.
A package to easily make use of Simple Icons in your Laravel Blade views.

Blade Simple Icons A package to easily make use of Simple Icons in your Laravel Blade views. For a full list of available icons see the SVG directory.

Cagilo - a set of simple components for use in your views Laravel Blade.

Cagilo - a set of simple components for use in your views Laravel Blade. Official Documentation Documentation for Cagilo can be found on its we

Use Blade templates without the full Laravel framework

blade Use Laravel Blade templates as a standalone component without the full Laravel framework Full documentation is available at http://duncan3dc.git

A package to easily make use of SVG icons in your Laravel Blade views.
A package to easily make use of SVG icons in your Laravel Blade views.

Blade Icons A package to easily make use of SVG icons in your Laravel Blade views. Originally "Blade SVG" by Adam Wathan. Turn... !-- camera.svg --

A package to easily make use of Iconsax in your Laravel Blade views.
A package to easily make use of Iconsax in your Laravel Blade views.

Blade Iconsax A package to easily make use of Iconsax in your Laravel Blade views. This package contains 1.000 icons in 6 diferent styles, a total of

Bunny CLI - Replicate and store your files to the edge!

Bunny CLI - Replicate and store your files to the edge! What is Bunny CLI? Bunny CLI is a tool for the console to upload frontend frameworks such as A

Comments
  • [V3] - update to laravel zero

    [V3] - update to laravel zero

    As much as i wanted to avoid a major update so soon, Im moving forward with a v3 for a couple of reasons.

    • Much of the code currently feels like is not responsible by this cli that includes the extended blade engine itself and so most of the code has been ported out and the clil is now going to consume the following packages: https://github.com/surgiie/console and https://github.com/surgiie/blade
    • Up until this point changelog has not been kept and sem versioning has not been properly followed or has been pretty loose up to this point. This mostly a lack of discipline on my part since im sole developer on this package so I believe i have not held myself against the proper standard. Therefore starting v3, a change log and proper sem versioning will be followed.
    • Updated package to a laravel zero app. There are many benefits to this, most noticeable is the robust components that laravel zero offers as the cli starts to get more complicated/add features, this will be helpful. Testing the commands is a little more seamless as well due to the testing utilities provided by Laravel/Laravel Zero.
    opened by surgiie 0
  • Improve test file write and add test for directory render.

    Improve test file write and add test for directory render.

    • Improve Blade::putTestFile to mkdir recursively for nested file values.
    • Adds test for render command call on a directory.
    • Cleanup exception handling in RenderCommand.
    opened by surgiie 0
  • Testing Refactors & Utilize SplFileInfo for file metadata needs.

    Testing Refactors & Utilize SplFileInfo for file metadata needs.

    • Reworks test suite to utilize new Blade testing helpers. Blade::assertRendered|putTestFile|testPath etc.
    • Remove deriveFileMetadata and utilize SplFileInfo object for the file being rendered.
    • Remove NormalizesPaths trait, SplFileInfo does this for paths.
    • General code cleanups.
    • Readme cleanups & document unit testing helpers
    • Remove unneeded extended code
    • Add CouldntWriteFileException
    • Regenerate completion script and increment to 0.1.0 for release.
    opened by surgiie 0
Releases(v3.7.0)
  • v3.7.0(Dec 27, 2022)

  • v3.6.0(Dec 27, 2022)

  • v3.5.0(Dec 24, 2022)

  • v3.4.3(Dec 14, 2022)

    • Minor change to how version number is determined, git tag method provided by laravel zero is unreliable as it always prints out unreleased
    Source code(tar.gz)
    Source code(zip)
  • v3.4.2(Dec 8, 2022)

  • v3.4.0(Nov 17, 2022)

  • v3.3.0(Nov 15, 2022)

  • v3.2.0(Nov 12, 2022)

  • v3.1.0(Nov 11, 2022)

    v3.1.0 - 2022-11-09

    • Remove runTask usages and update surgiie/console due to overhead and bad flicker from spinner process by @surgiie in https://github.com/surgiie/blade-cli/pull/12
    Source code(tar.gz)
    Source code(zip)
  • v3.0.0(Nov 10, 2022)

    v3.0.0 - 2022-11-08

    Changed

    • CLI application has been migrated to laravel-zero/laravel-zero by @surgiie in https://github.com/surgiie/blade-cli/pull/10
    • Blade classes/componens have been extracted to standalone surgiie/blade package and direct use of the class is no longer part of this package by @surgiie in https://github.com/surgiie/blade-cli/pull/10
    • Validation of path is now handled by rules validation via the surgiie/console package by @surgiie in https://github.com/surgiie/blade-cli/pull/10
    • --save-dir and --save-as have been removed in favor of a single --save-to option by @surgiie in https://github.com/surgiie/blade-cli/pull/10

    Added

    • Application now utilizes surgiie/console package which includes surgiie/transformer and laravel validation functionality by @surgiie in https://github.com/surgiie/blade-cli/pull/10

    • --dry-run now supported on directory rendering by @surgiie in https://github.com/surgiie/blade-cli/pull/10

    • clear command to clear compiled files by @surgiie in https://github.com/surgiie/blade-cli/pull/10

    Source code(tar.gz)
    Source code(zip)
  • v2.0.7(Oct 5, 2022)

  • v2.0.6(Oct 3, 2022)

    • Move/rename Surgiie\BladeCLI\Support\ConsoleApplication to Surgiie\BladeCLI\Application
    • Minor polishes/typo fixes, including removing doc block annotations.
    Source code(tar.gz)
    Source code(zip)
  • v2.0.5(Sep 23, 2022)

  • v2.0.3(Sep 22, 2022)

  • v2.0.0(Aug 19, 2022)

    • Remove --save-directory in place of two new options for saving: --save-as for files and --save-dir for directory.
    • Enforce --save-dir for directory rendering.
    • Testing/faking improvements
    • Add support to use .env files for variable data with new --from-env option.
    • Aesthetics/typehints
    Source code(tar.gz)
    Source code(zip)
  • v1.0.3(Aug 1, 2022)

    • Allow file path to be set later by adding default argument of null
    • Add test for new exception thrown if no filepath is set when render is attempted.
    Source code(tar.gz)
    Source code(zip)
  • v1.0.2(Jul 31, 2022)

  • v1.0.1(Jul 10, 2022)

  • v1.0.0(Jul 9, 2022)

  • v0.1.2(Jun 17, 2022)

  • v0.1.1(Jun 14, 2022)

    • Fix Blade::putTestFile to mkdir recursively for nested file values.
    • Adds test for render command call on a directory.
    • Cleanup exception handling in RenderCommand.
    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Jun 13, 2022)

    • Reworks test suite to utilize new Blade testing helpers. Blade::assertRendered|putTestFile|testPath etc.
    • Remove deriveFileMetadata from blade class and utilize SplFileInfo object for the file metadata.
    • Remove NormalizesPaths trait.
    • Friendlier errors instead of exceptions during render command line calls. Exceptions still raised during tests to make assertions.
    • General code cleanups.
    • Readme cleanups & document unit testing helpers
    • Remove unneeded extended code
    • Add CouldntWriteFileException for when saving rendered files fails.
    • Regenerate completion script.
    Source code(tar.gz)
    Source code(zip)
  • v0.0.6(Jun 12, 2022)

Owner
Sergio Compean
I make stuff.
Sergio Compean
Composer package which adds support for HTML5 elements using Laravels Form interface (e.g. Form::date())

Laravel HTML 5 Inputs Composer package which adds support for HTML5 elements by extending Laravel's Form interface (e.g. Form::date()) Adds support fo

Small Dog Studios 11 Oct 13, 2020
A package to flash multiple messages using Laravels default session message flashing system

Flash multiple advanced messages with both text, messages and links An opinionated solution for flashing multiple advanced messages from the backend a

Bilfeldt 6 Jan 18, 2022
Adds a way to write php and run it directly in Laravels' Artisan Tinker.

Adds a way to write php in PhpStorm/IDEA and run it directly as if through laravel artisan tinker - allowing you to quickly run a piece of code with a

Robbin 120 Jan 2, 2023
Use Laravel's Blade templating engine outside of Laravel.

Use Laravel's Blade templating engine outside of Laravel. This package provides a standalone version of Laravel's Blade templating engine for use outs

Ryan Chandler 22 Jan 2, 2023
Blade UI Kit is a set of renderless components to utilise in your Laravel Blade views

Blade UI Kit is a set of renderless components to utilise in your Laravel Blade views. In all essence, it's a collection of useful utilities, connecting the dots between different parts of the TALL stack. It was made for Blade, Laravel's powerful templating engine.

Blade UI Kit 1.2k Jan 5, 2023
A Laravel package that allows you to use multiple ".env" files in a precedent manner. Use ".env" files per domain (multi-tentant)!

Laravel Multi ENVs Use multiple .envs files and have a chain of precedence for the environment variables in these different .envs files. Use the .env

Allyson Silva 48 Dec 29, 2022
A Laravel Code Generator based on your Models using Blade Template Engine

Laravel Code Generator is a PHP Laravel Package that uses Blade template engine to generate code for you. The difference between other code generators

Victor Yoalli 59 Dec 5, 2022
Snippets for blade template engine

Blade Snippets for Sublime Text Blade is a simple, yet powerful templating engine provided with Laravel PHP framework. These snippets works with blade

Alex Antonyuk 113 Jan 3, 2023
This package adds syntax definitions for the Laravel Blade engine.

Laravel Blade Highlighter This package adds syntax definitions for the Laravel Blade engine. Works with various Sublime Text version, for older/specif

Eric Percifield 393 Dec 24, 2022
A general-purpose parser for Laravel's Blade templating engine.

A general-purpose parser for Laravel's Blade templating engine. This is where your description should go. Try and limit it to a paragraph or two. Cons

Ryan Chandler 6 Feb 18, 2022