PHP View and laravel blade template
For views, we can easily do
<?php echo $var; ?>
or use <?= $var; ?>
which is equivalent as well, to show a variable.
To send data, first, you can send the data
directly from the web.php
, with such intuitively.
Route::get('/', function() {
return view('welcome', [
'var' => 'something important'
]);
});
This might get confusing at first,
and I did too, because the variable name
is matching the string, which is not that common thing
in my experience.
But I guess this is laravel’s conventions of passing data.
Also, there is a php function used very much dealing with this,
which is compact()
, when you have the variable outside the fucntion,
but it has to match the name. For example,
$var = 'something important';
Route::get('/', function() {
return view('welcome', compact('var'));
});
Which will do the exact same thing as above.
We can have more than one variables in the compact function.
Inside the blade template,
we could use php logics, such as foreach, if, else, while,
those sort of logics.
This is the example of using foreach.
<ul>
<?php foreach ($tasks as $task) : ?>
<li><?= $task; ?></li>
<?php endforeach; ?>
</ul>
This is pure vanila php, but we can surely make this simple, using blade template.
<ul>
@foreach ($tasks as $task)
<li>{{ $task }}</li>
@endforeach
</ul>