LARAVEL Blade Templates & Views 1 — Questions and Answers
Question 1: Which Blade syntax outputs a variable with automatic HTML escaping?
- {!! $var !!}
- {{ $var }} (Correct answer)
- @echo($var)
- @print($var)
Correct answer: {{ $var }}
Double curly braces {{ $var }} automatically escape HTML entities to prevent XSS attacks.
Question 2: Which Blade directive is used to extend a parent layout?
- @include
- @layout
- @extends (Correct answer)
- @parent
Correct answer: @extends
@extends('layout-name') tells Blade that the current view inherits from a parent layout.
Question 3: Which Blade directive marks the beginning of a content section in a child view?
- @content
- @section (Correct answer)
- @block
- @yield
Correct answer: @section
@section('name') defines a block of content that will be injected into the parent layout.
Question 4: Which Blade directive in a parent layout outputs content defined in child view sections?
- @section
- @show
- @yield (Correct answer)
- @render
Correct answer: @yield
@yield('section-name') in a parent layout renders whatever content a child view injects into that named section.
Question 5: How do you include a Blade sub-view (partial) inside another Blade template?
- @extend('partial')
- @include('partial') (Correct answer)
- @render('partial')
- @use('partial')
Correct answer: @include('partial')
@include('view.name') embeds another Blade view at that location and shares all variables from the parent scope.
Question 6: Which Blade syntax outputs a variable as raw, unescaped HTML?
- {{ $html }}
- {!! $html !!} (Correct answer)
- @raw($html)
- @html($html)
Correct answer: {!! $html !!}
{!! $html !!} outputs the value without HTML escaping, which is needed when rendering trusted HTML content.
Question 7: Which Blade directive is used for basic conditional rendering?
- @when
- @if (Correct answer)
- @check
- @condition
Correct answer: @if
@if(condition) ... @endif is the standard Blade directive for conditional output, mirroring PHP's if statement.
Which Blade syntax outputs a variable with automatic HTML escaping?