quinta-feira, 8 de dezembro de 2016

How to use Blade Without Laravel

Blade is so nice, but sometimes you have to work on legacy projects or with frameworks not friendly. So, looking at the web i found the windwalker that until now is doing great. So, here i'll show a basic step-by-step to use Blade without Laravel. First, we'll configure composer, so install composer if you don't have it.
Create the composer.json:
 {  
      "autoload":{  
           "psr-4":{  
                "App\\": "app",  
                "Windwalker\\": "vendor/windwalker"  
           }  
      },  
      "require": {  
     "windwalker/renderer": "2.*",  
     "illuminate/view": "4.*"  
   }  
 }  
Here, i set the namespace for my work environment: App and Windwalker to use in my initial load. The require pull the libs. Now composer install(if you did not have) && composer update(if you updated) && composer auto-load
Create a folder init and write blade.php:
 <?php   
 use Windwalker\Renderer\BladeRenderer;  
 $paths = array('./view');  
 $renderer = new BladeRenderer($paths, array('cache_path' => './cache'));  
Set the path to your view folder and to a cache folder.
 <?php  
 require 'vendor/autoload.php';  
 require 'init/blade.php';  
 $data = ['title' => 'Nice!'];  
 $html = $renderer->render('index', $data);  
 echo $html;  

Create index.blade.php file in view folder.
 @section('title')  
      @if( isset($title) )  
           {{ $title }}  
      @else  
           Without Title  
      @endif       
 @stop  
 <html>  
 <head>  
 <title>@yield('title')</title>  
 </head>  
 <body>  
 <p>Body</p>  
 @include('footer')  
 </body>  
 </html>  
I created a footer.blade.php file to show the include.
Check this on: Github
References:
http://asika.windspeaker.co/post/3975-use-blade-template-engine-outside-laravel
https://packagist.org/packages/philo/laravel-blade

Nenhum comentário:

Postar um comentário