Can I use Laravel without a blade?

Setting Variables in a Laravel Blade Template

I'm reading the Laravel Blade documentation and can't figure out how to assign variables in a template for later use. I can't do it because it represents "whatever" and I don't want that.

I understand I can do, but that's not elegant.

What better and more elegant way to do this in a blade template?


It is not recommended to do this in a view that does not have a blade tag for it. If you want to do this in your blade view, you can either just open a PHP tag as you are writing it or register a new blade tag. Just an example:


The @ php blade directive no longer accepts inline tags. Instead, use the full form of the policy:


You can just use:

Or you can use it in a block statement:


Extend the blade as follows:

Then do one of the following:

Quick solution: If you are lazy, just enter the code into the boot () function of the AppServiceProvider.php.

Better solution: create your own service provider. Visit to learn how to expand the blade in Laravel 5. This way it's a little more work, but good practice in using providers :)


You can just put the above code in below app / start / global.php (or elsewhere if you think this is better) insert .

After making the above changes, you can use:

define a variable.

In the Laravel-4 you can use the template comment syntax to define / set variables.

Comment syntax is and is rendered by Blade Engine as

With a little trick we can use it to define variables, for example

is rendered by Kling as what the variable sets for us. Without changing a line of code.

There is a simple workaround that doesn't require you to change any code, and it works in Laravel 4 too.

You simply use an assignment operator () in the expression passed to a statement instead of (for example) an operator such as

Then you can use it anywhere you can use another variable

The only downside is that your job is for someone who doesn't know you are doing this as a workaround , like a mistake looks like .

You're going to make it too complicated.

Just use PHP


(or looks pretty simple too)

We all "hack" the system by setting variables in views. So why make the "hack" more complicated than it has to be?

Tested in Laravel 4.

Another benefit is that the syntax highlighting works properly (I used a comment hack before and it was horrible to read).

You can set variables in the blade templating engine in the following ways:

1. General
Variable for setting PHP blocks :

Variable for setting Blade PHP blocks :

As of Laravel 5.2.23, you have the @ php blade directive, which you can use inline or as a block directive:


You can set a variable in the view file, but it will be printed exactly as you set it. In any case, there is a workaround. You can set the variable in an unused section. Example:

Then it prints wherever you want, but you won't get the output if you save the variable.

EDIT: The The section must be named, otherwise an exception is thrown.

In Laravel 4:

If you want the variable to be available in all of your views, not just your template, this is a great method (more on this blog).

Add just the following in app / controller / BaseController.php

and now it's available to all of your views - including your template.

I used this to set environment specific asset urls for my images.

And suddenly nothing will appear anymore. In my experience, when you need to do something like this, prepare the HTML in a model's method, or reorganize your code into arrays or something like that.

There is never just one way.

I'll expand on @Pim's answer.

Add this to the startup method of your AppServiceProvider

You cannot write a PHP expression this way.

You can use this directive as follows:

In Laravel 5.1, 5.2 :

You may need a data item for all views share that will be rendered by your application. You can do this using the view factory's sharing method. Typically, you should place calls for approval within a service provider's startup method. You can add them to the AppServiceProvider or generate a separate service provider to accommodate them.

Edit file:

My elegant way is as follows

And just return your variable.

If you PHP 7.0 have:

The easiest and most effective way is mapping in brackets .

The rule is simple: do you use your variable multiple times? Then, the first time it is used in parentheses, explain it, keep calm and move on.

And yes, I know about it, this is just a demo.

Since your variables are now declared as and when they are in use, no blade workarounds are required.

I don't think you can - but then again, that kind of logic should probably be handled in your controller and passed to the view already established.

Assign a variable to the blade template. Here are the solutions

We can use tag in blade side


We can use the blade comment with special syntax

Hacking comments is not a very readable method. Editors also color it as a comment and someone may miss it when looking through the code.

Try something like this:

It is compiled into:

... and do the job and do nothing.

It is better to practice defining variables in controller and then passing them to the view with or method.

Otherwise, #TLGreg gave the best answer.

There is a very good extension for Blade Radic / Blade Extensions. After adding it, you can use @set (variable name, variable value).

I was looking for a way to assign a value to a key and, in my opinion, use it multiple times. In this case, you can first use the value and then call it to print the value elsewhere in your view or its child.

In my opinion, it is better to leave the logic in control and pass it to the view to be used. This can be done in two ways with the 'View :: make' method. I'm currently using Laravel 3, but I'm pretty sure this will be the same in Laravel 4.