laravel

Assigning a Variable in a Blade Template

Assigning a Variable in a Blade Template

Problem

You want to assign a variable in a Blade template.

Solution

Blade does not provide a command to do this.

The idea is to cleanly separate logic from presentation. But in the case where it's more expedient to assign a variable in a template, here's a couple tricks.

You can always use the PHP tags.


$var

Or, you can use a Blade comment with a special syntax.

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

This second method works because Blade comments get translated in the format below.

Thus, the above variable assignment gets translated to the following PHP code.

See Using Comments in Blade Templates.

Discussion

You also extend Blade adding a new command, such as @setvar.

See the Extending Blade Templates recipe.

Kako uporabljati AutoKey za avtomatizacijo iger Linux
AutoKey je namizni pripomoček za avtomatizacijo za Linux in X11, programiran v Python 3, GTK in Qt. Z njegovo funkcijo skriptiranja in MACRO lahko avt...
Kako prikazati števec FPS v igrah Linux
Linux gaming je dobil velik zagon, ko je Valve leta 2012 napovedal podporo Linuxu za odjemalca Steam in njihove igre. Od takrat se je veliko AAA in in...
Kako prenesti in igrati Sid Meier's Civilization VI v Linuxu
Uvod v igro Civilization 6 je sodoben pogled na klasični koncept, uveden v seriji iger Age of Empires. Ideja je bila dokaj preprosta; začeli bi v zelo...