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.

A League of Legends telepítése az Ubuntu 14-re.04
Ha rajongsz a League of Legendsért, akkor ez egy lehetőség arra, hogy teszteld a League of Legends játékot. Ne feledje, hogy a LOL a PlayOnLinux rends...
Telepítse a legújabb OpenRA stratégiai játékot az Ubuntu Linux rendszerre
Az OpenRA egy ingyenes / ingyenes valós idejű stratégiai játékmotor, amely újjáépíti a korai Westwood játékokat, mint például a klasszikus Command & C...
Telepítse a legújabb Dolphin Emulator for Gamecube & Wii szoftvert Linuxra
A Dolphin Emulator segítségével a kiválasztott Gamecube & Wii játékokat játszhatja Linux személyi számítógépeken (PC). Mivel a Dolphin Emulator szaba...