Write a readable and easier Blade in Laravel

Image for post
Image for post

Always I hated to write some helper functions and some PHP Pure codes in Laravel Blade and after a while, I decided to write a Laravel package to create an easier Blade.

This package has some directive which can help you to write a Blade with more directive.

With the EasyBlade package, You don’t need to write more PHP Pure code and after installing this package You will be able to write a better and more understandable Blade (view).

Let’s see some directives of this package.

  • The first directive is @route :
//Before : {{ route(‘home’) }}//After : @route(‘home’)

It does the same thing as the route helper function in Laravel in you can pass a route name to this directive as the first parameter and it will return the full address of the route.

  • The second directive is @url :
//Before :  {{ url('/') }}//After : @url('/')
  • The next one is @asset :
//Before :  {{ asset('/img/header.png') }}//After : @asset('/img/header.png')

There are more directives that enable you to not write extra PHP code.

Let me show You

  • One of these cool directives is @isActive :

You may want to print a string when your client is in a special route, Imagine you want to print ‘enable’ string to a class when your client is in ‘home’ or ‘home.about’ route (to add some style with CSS you should use a class name and in this example this class is active and disabler class is deactive).

Let’s write this example with EasyBlade

@isActive(['home', 'home.about'], 'active', 'deactive')// if current route equals to 'home' or 'home.about' it will print 
//active text and if not it will print deactive

It was too easy, was it not?

Imagine you want to execute some codes if the count of array or collection is equal or greater than an int, You should use if() and count() function, We have created it easier.

  • Just write @count directive :
@count($users, 1)
// Do something
@endcount
// It will compile to :if(count($users) >= 1){
// Do Something ...
}

Lots of time you have used something like :

auth()->user()->name

Let me show you something easier, Just use @user(‘name’) :

@user(‘name’)// or@user(name)

It will write the same thing like auth()->user()->name.

I hope you use this package and if you have any idea to improve the package please tell us as a comment or if You have enough knowledge send it as a pull request on Github (I’m waiting for you… 😊 ).

Github Repository :

https://github.com/rezaamini-ir/laravel-easyblade

Don’t forget to star Repository⭐

Written by

A young full-stack Developer who loves Laravel and reading.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store