Bird
0
0

How should you separate logic and presentation?

hard📝 Application Q8 of 15
Laravel - Views and Blade Templates
You want to display a list of products with their prices in a Blade template. The controller sends an array $products where each product has 'name' and 'price'. How should you separate logic and presentation?
ACalculate total price in the controller, loop and display in the template
BCalculate total price inside the Blade template using @php
CWrite all logic and display code inside the template
DUse JavaScript in the template to calculate prices
Step-by-Step Solution
Solution:
  1. Step 1: Identify where calculations belong

    Business logic like calculating totals should be done in the controller.
  2. Step 2: Use the template only for display

    The template loops through data and shows it without extra calculations.
  3. Final Answer:

    Calculate total price in the controller, loop and display in the template -> Option A
  4. Quick Check:

    Logic in controller, display in template [OK]
Quick Trick: Keep calculations in controller, templates for display [OK]
Common Mistakes:
  • Doing calculations in templates
  • Mixing logic and display code
  • Using JavaScript for server data calculations

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes