Gesparo
11/9/2018 - 12:52 PM

Svg blade directive

Add new directive for blade that helps sefaly add svg files into blade template. I don't use simple file_get_contents function because sometimes file in path can not be attached because file system is bisy or do not answer

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // use @svg(path/to/file.svg)
        // @TODO After adding this directive you should call 'php artisan view:clear' command
        Blade::directive('svg', function($path) {
            $pathToFile = public_path($path);
            $svg = ! file_exists($pathToFile) ? '' : file_get_contents($pathToFile);

            return "<?php echo '$svg'; ?>";
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}