Reputation: 1
I hope you are well. I am currently learning Laravel 8, and am of course encountering some small complications while learning. I would like to understand the following:
What does "{{ ... }}" mean? I understand the principle, of putting PHP in HTML, but how does it work? What does the "{{ }}" mean?
What does "@if", "@foreach" mean? I also understand the principle of putting a conditional aspect within a file, but what does "@" represent?
I have a particular problem, and when I search on the internet, I quickly come across PHP version problems (and I confess I don't believe it):
For example, I make a complete pagination system, and when I set up a foreach to set up my page numbers, it returns me an error:
@if ($paginator->hasPages())
<ul class="pagination">
@foreach ($elements as $element)
@if (is_string($element))
<li class="page-item disabled"><span class="page-link">{{ $element }}</span></li>
@endif
@if (is_array($element))
@foreach ($element as $page => $url)
<li class="page-item">
<a href="{{ $url }}" class="page-link">{{ $page }}</a>
</li>
@endforeach
@endif
@endforeach
</ul>
@endif
Now, if I put this:
@if ($paginator->hasPages())
<ul class="pagination">
@foreach ($elements as $element)
@if (is_string($element))
<li class="page-item disabled"><span class="page-link">{{ $element }}</span></li>
@endif
@if (is_array($element))
@foreach ($element as $page => $url)
<li class="page-item">
<a href="{{ $url ?? '' }}" class="page-link">{{ $page ?? '' }}</a>
</li>
@endforeach
@endif
@endforeach
</ul>
@endif
Now it works properly. If I replace the elements like the first text quote, then it's no longer a problem...
Could it be the cache? From the update of the server pages without restarting it with Laravel 8?
I thank you in advance for your help. I come here as a last resort, without knowing what to write on the internet, without finding a very logical and coherent answer...
Thanks 🙏
Typical example of an error that I have right away:
ParseError
syntax error, unexpected ' ' (T_STRING), expecting ')' (View: /Applications/MAMP/htdocs/Laravel/Site/resources/views/vendor/pagination/custom.blade.php)
In the code, I wrote this:
@if ($paginator->onFirstPage())
@else
<li class="page-item">
<a href="{{ $paginator->previousPageUrl() }}" rel="prev" class="page-link">
Précédent
</a>
</li>
@endif
For it to work I have to put : {{ $paginator->previousPageUrl() ?? '' }}
Upvotes: 0
Views: 61
Reputation: 3201
Those "{{ ... }}" and "@" are just statements by which laravel template engine called blade knows what to do with provided statement.
You can read documentation of blade here.
Blade's
{{ }}
echo statements are automatically sent through PHP'shtmlspecialchars
function to prevent XSS attacks.
"{{ ... }}" is called echo statement. So:
{{ $variable }}
is translated to something like this:
<?php echo htmlspecialchars($variable )?>
@if
, @for
, @foreach
are replaced to something like this:
<?php foreach($variable as $key => $value) { ?>
// template that you put inside
<?php } ?>
and so on.
Errors with your functions that you claim that you have to put ?? ''
are caused that probably value of function / function itself is null
.
The ??
operator checks for nulls (what you can read here):
The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not null; otherwise it returns its second operand.
Upvotes: 3