Challenge 2 — Solution
Task: Write a Blade template fragment that loops over an array of
$products (each with 'name' and 'price' keys) and displays them in a
list, using Blade's @foreach/@endforeach and {{ }} syntax. Explain in a
comment exactly what security protection {{ }} provides automatically
that this course's plain-PHP views always had to apply manually.
---- resources/views/products/index.blade.php ----
@foreach ($products as $product)
- {{ $product['name'] }} - £{{ $product['price'] }}
@endforeach
{{-- What {{ }} provides automatically:
Blade's {{ }} syntax calls htmlspecialchars() on the value
automatically before outputting it - exactly the same escaping
this course's own plain-PHP views (e.g. post_list.php in Chapter 3)
always had to apply manually, via
= htmlspecialchars($post['title']) ?>, on every single echoed
value. Using {{ $product['name'] }} instead of a raw
= $product['name'] ?> means an XSS attempt (e.g. a product name
containing a