Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP code inside a Laravel 5 blade template

I have to place some PHP code inside a Laravel 5 blade template. Like below

@foreach ($farmer->tasks as $task)
    @if ($task->pivot->due_at) < date(now))
        $style = 'alert alert-danger';
    @elseif ($task->pivot->due_at) > date(now))
        $style = 'alert alert-success';
    @else
        $style = '';
    @endif
@endforeach

What is the actual procedure to place PHP code inside a Laravel 5 blade template?

like image 931
abu abu Avatar asked Jun 27 '15 10:06

abu abu


People also ask

Can we write PHP code in Laravel?

In this tutorial, we will learn how to write php code in laravel blade file. In laravel blade file we add the php code some different way; just like we use to start @php and @endphp to end the php code.

What is Blade PHP in Laravel?

Introduction. Blade is the simple, yet powerful templating engine that is included with Laravel. Unlike some PHP templating engines, Blade does not restrict you from using plain PHP code in your templates.

Can I use Laravel without blade?

<? php echo $name ?> <? php Section::stop(); ?>


5 Answers

According to documentation, in Laravel 5.2 and newer you can use the following code:

@php
{{-- PHP code here --}}
@endphp

Alternatively, you can extend the Blade templating engine as it's described here.

If neither of the above solutions is suitable, you are stuck with the answers given by Armen and by Gonzalo.

like image 187
user28864 Avatar answered Oct 01 '22 15:10

user28864


Just open and close PHP tags:

<?php $style = '...'; ?>
like image 40
Armen Markossyan Avatar answered Oct 01 '22 16:10

Armen Markossyan


In modern Laravel (6/7) you should do this:

@php
   yourphpcode();
@endphp
like image 38
Serg_x Avatar answered Oct 01 '22 16:10

Serg_x


The following new NewBladeCompiler will use @{ }} for accepting all PHP code like variable assigning, class declaration, etc.

E.g. @{ $variable = 0; }} will be compiled to <?php $variable=0; ?>

<?php

    use Illuminate\View\Compilers\BladeCompiler;

    class NewBladeCompiler extends BladeCompiler
    {

        /**
         * Get the echo methods in the proper order for compilation.
         *
         * @return array
         */
        function getEchoMethods()
        {
            $methods = [
                'compileRawEchos'     => strlen(stripcslashes($this->rawTags[0])),
                'compileEscapedEchos' => strlen(stripcslashes($this->escapedTags[0])),
                'compileRegularEchos' => strlen(stripcslashes($this->contentTags[0])),
                'compilePhpEchos'     => strlen(stripcslashes("@{"))
            ];

            uksort($methods, function ($method1, $method2) use ($methods) {

                // Ensure the longest tags are processed first
                if($methods[$method1] > $methods[$method2])
                {
                    return -1;
                }
                if($methods[$method1] < $methods[$method2])
                {
                    return 1;
                }

                // Otherwise give preference to raw tags (assuming they've overridden)
                if($method1 === 'compilePhpEchos')
                {
                    return -1;
                }
                if($method2 === 'compilePhpEchos')
                {
                    return 1;
                }
                if($method1 === 'compileRawEchos')
                {
                    return -1;
                }
                if($method2 === 'compileRawEchos')
                {
                    return 1;
                }
                if($method1 === 'compileEscapedEchos')
                {
                    return -1;
                }
                if($method2 === 'compileEscapedEchos')
                {
                    return 1;
                }
            });

            return $methods;
        }

        function compilePhpEchos( $value )
        {
            $pattern  = sprintf('/(@)?%s\s*(.+?)\s*%s(\r?\n)?/s', "@{", "}}");
            $callback = function ($matches) {
                $whitespace = empty($matches[3]) ? '' : $matches[3] . $matches[3];
                return $matches[1] ? substr($matches[0], 1) : '<?php ' . $matches[2] . ' ?>' . $whitespace;
            };
            return preg_replace_callback($pattern, $callback, $value);
        }

    }

?>
like image 30
Elitexp Avatar answered Oct 01 '22 14:10

Elitexp


Laravel recipes suggest a simple, but effective, way to do it without including the PHP tags:

{{--*/ $var = 'test' /*--}}

{{-- --}} works as a blade comment / and / reverts the effect of comment resulting on

<?php $var = 'test' ?>

The problem is that is longer than including PHP tags :-(

like image 22
Gonzalo Cao Avatar answered Oct 01 '22 14:10

Gonzalo Cao