Showing posts with label new Features. Show all posts
Showing posts with label new Features. Show all posts

Monday, 6 November 2017

Blade with View::first new Features in Laravel5.5

When building dynamic components or pages sometimes we want to display a custom template if it exists or otherwise falls back on a default one.
We can solve this problem with a series of conditionals or by using view()->exists() to check if a custom template exists or not, however, Laravel 5.5 brings us a better and more elegant way as I’ll demonstrate in the following video:
https://www.youtube.com/watch?v=n32SwdlvAPI

Using View:: first

The view()->first() method allows us to replace the following code:
if (view()->exists('template1')) {
    return view('template1', $data);
}

return view('1template2', $data);
With a simpler, more expressive version:
return view()->first(
    ['template1', 'template2'], $data
);
You have to pass an array of templates as the first argument and the first method will load the first template it finds.
Of course, you can pass as many templates as you want and even use dynamic names:
return view()->first([
    "pages/{$page->slug}",
    "pages/category-{$page->category->slug}",
    "pages/default-template"
], $data);
Remember you can also use this feature using its facade version:
\View::first($templates, $data)
This dynamic view loading feature was added to Blade in Laravel v5.5 and is a great way of keeping your controllers simple by avoiding extra conditionals when dealing with dynamic templates.

Sunday, 5 November 2017

Laravel new Features in Blade If Directives

A new Blade addition in Laravel 5.5 will add support for simplifying custom ifstatements in your views.
The syntax might something like this in your AppServiceProvider::boot()method:
use Illuminate\Support\Facades\Blade;

Blade::if('admin', function () {
    return auth()->check() && auth()->user()->isAdmin();
});
The new Blade::if() makes it convenient to abstract repetitive checks out of templates, making them more readable:
@admin
    <a href="{{ route('super.secret') }}">Secret Page</a>
@else
    Welcome Guest. <a href="{{ route('login') }}">Login</a>
@endadmin
In previous versions of Laravel, you would have to write a bit more code. For example, David Hemphill tweeted some really cool directives using this technique in Laravel 5.4:
//AppServiceProvide
Blade::directive('prod',function($beta){
 return "<?php if(app()->environment('production')):?>";
});
Blade::directive('endprod',function($beta){
 return "<?php endif; ?>";
});

-----------------------------------
//In your blade template
@prod
<script src="some-production-only-script" async></script>
@endprod
Which is now simplified even more in Laravel 5.5:
Blade::if('prod', function () {
    return app()->environment('production');
});
You can also pass arguments to make the checks more dynamic:
Blade::if('env', function ($env) {
    return app()->environment($env);
});
Which would then look like this in your templates:
@env('production')
  <script src="some-prod.js"></script>
@endenv
If you want to learn more about Blade::if(), Laracasts has a video tutorial on it, and we look forward to seeing what you’ll come up with!

cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) in Laravel

  WampServer: Download this file:  http://curl.haxx.se/ca/cacert.pem Place this file in the  C:\wamp64\bin\php\php7.1.9  folder Open  php.in...

Popular Articles