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.

Топ 10 игри за игра на Ubuntu
Платформата на Windows е една от доминиращите платформи за игри поради огромния процент игри, които се развиват днес, за да поддържат Windows. Може ли...
5 най-добри аркадни игри за Linux
В днешно време компютрите са сериозни машини, използвани за игри. Ако не можете да получите новия висок резултат, ще разберете какво имам предвид. В т...
Битка за Уеснот 1.13.6 Разработка освободена
Битка за Уеснот 1.13.6, издаден миналия месец, е шестата версия за разработка в 1.13.x серия и предоставя редица подобрения, най-вече на потребителски...