0

Blade views in Laravel provide many control structures (if, loops, switches) using the @ symbol which seem to work well, but I'm wondering if there's any advantage to using those over their equivalent raw php commands.

For example, in Laravel an if statement would look like:

@if(statement)
    some html
@endif 

and the equivalent raw php would look like:

<? if(statement) {?>
    some html
<? } ?>

I'm upgrading an application to Laravel from a Zend Framework v1 backend and have been switching over to using laravel's control structures but am wondering if there's really an advantage other than sticking to laravel's standards?

BrandonO
  • 231
  • 2
  • 10
  • 3
    faster to write, more readable, gives u experience using templating engine, which everyone uses, because of the first 2 points. – Andrew Jan 07 '19 at 14:34
  • 1
    Yes, the point of a framework is to provide conventional ways of doing common tasks. Templating engines make code easier to write, read, and debug. If you're asking about speed/performance, don't waste your time here - you'll never gain any noticeable speed circumventing templating shortcuts. Spend your optimization efforts on things that will actually make a measurable difference in your code. – WillardSolutions Jan 07 '19 at 14:38
  • The `` short tag syntax has been recommended against for like a decade now, for starters. See https://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use. – ceejayoz Jan 07 '19 at 14:39

1 Answers1

4

As the docs says:

Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures, while also remaining familiar to their PHP counterparts.

As you can see, the main purpose is to create shorcuts that are "cleaner" when reading. A better API.


Update

As @ceejayoz mentioned, its also extendible. Blade let's you create your own directives to be used in your views. This directives can also receive parameters.

Extending Blade

Blade allows you to define your own custom directives using the directive method. When the Blade compiler encounters the custom directive, it will call the provided callback with the expression that the directive contains.

The following example creates a @datetime($var) directive which formats a given $var, which should be an instance of DateTime:

<?php

namespace App\Providers;

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

class AppServiceProvider extends ServiceProvider
{
    /**
     * Perform post-registration booting of services.
     *
     * @return void
     */
    public function boot()
    {
        Blade::directive('datetime', function ($expression) {
            return "<?php echo ($expression)->format('m/d/Y H:i'); ?>";
        });
    }

    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

As you can see, we will chain the format method onto whatever expression is passed into the directive. So, in this example, the final PHP generated by this directive will be:

<?php echo ($var)->format('m/d/Y H:i'); ?>
Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
  • 1
    Plus, it's extensible, so you can make your own `@dosomethingawesome` syntax that might do something complicated under the hood: https://laravel.com/docs/5.7/blade#extending-blade – ceejayoz Jan 07 '19 at 14:40
  • Nice @ceejayoz, I'll add it too. – Kenny Horna Jan 07 '19 at 14:45