Tim Habersack

Laravel; having a conditional choose your @extends template

I am firmly entrenched in the Laravel camp now. One thing I love about Laravel is their blade templating language. However, there is some quirkiness that isn’t immediately obvious.

Let’s say you have a template that is extending another template. You want a conditional to determine which template you want to extend. (The use case here for me is a messaging system. Logic + layout is the same on back-end and front-end, but they extend different templates).

I was trying to get this to work:



<h1>Here are things!</h1>


All it was doing was outputting the one @extends(‘base.base’) line. After much hunting, I found this great answer on StackOverflow. So, @extends() must be on the first line of a blade template, which is a little weird, but whatever. Used ternary operator like this and it works!

@extends($var ? 'account.base' : 'base.base')

Categories: Coding

» « It’s not the people you miss, it’s the moment.


  1. What do you do if you want to choose from multiple layouts? Without resorting to a nested ternary?

    • Tim Habersack

      2018.06.26 — 15:18:39

      That’s a good question; I haven’t had the need before. Let me know if you come up with a solution! I wrote this post a long time ago, but it still gets lots of traffic.

Leave a Reply

Your email address will not be published.


Copyright © 2019 Tim Habersack

Theme by Anders NorenUp ↑