How to use custom file extensions for Laravel Blade

Sam

Code for this tutorial can be found on GitHub

Laravel

PHP

Laravel Blade is a great templating engine that ships with Laravel, it's mainly designed to be used with .php files but it can be used with other file extensions with ease. We use Blade extensively at Vizalo for templating commonly run scripts on servers. For example our firewall management tool runs a number of scripts to add or remove firewall rules using UFW, these scripts are all built with Laravel Blade.

Service Providers to the rescue

So you want to use Laravel Blade for templating shell scripts like we do at Vizalo. It's really simple actually using Laravel's amazing Service Providers.

In your AppServiceProvider.php you can add the following line inside of the boot() method:

View::addExtension('blade.sh', 'blade');

So what is happening here? We're essentially telling Laravel that anytime we pass it a template containing .blade.sh as the file extension to use the blade engine. This means you could write a script called echo-my-name.blade.sh and pass it a variable called $name and run a bash script like this:

echo {{ $name }}

Amazing right?!

Wrapping up

This is our first post of 2025 and it's short and sweet. Hope this helps you in your Laravel adventures, have a great day and a happy new year!

Table of Contents