Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add extendsFirst method similar to includesFirst #34648

Merged
merged 1 commit into from
Oct 5, 2020

Conversation

horphoz
Copy link
Contributor

@horphoz horphoz commented Oct 3, 2020

The reason / need:
To extends a template according to a conditional existance.
For example to use different templates according to some configuration or variable.
To give an example let's have an idea of a marketplace API that sells products for a variety of stores.
So each store could have their own "base template" or in case they don't the API can use the default one.

Example that doesn't work today (using IFs):

@if(\View::exists('emails.store.' . $storeId)
@extends('emails.store.' . $storeId)
@else
@extends('emails.store.default')
@endif

The actual problem of using this way:
The compiler doesn't use that conditional IF and the content will be showed in double case both files exists. So the content is duplicated! When the view is rendered it returns a duplicated layout =/

The solution:
Added an extendsFirst method on CompilesLayouts using the "first" method

@GrahamCampbell GrahamCampbell changed the title extendsFirst method the same way as the includesFirst [8.x] extendsFirst method the same way as the includesFirst Oct 3, 2020
@GrahamCampbell GrahamCampbell changed the title [8.x] extendsFirst method the same way as the includesFirst [8.x] Add extendsFirst method similar to includesFirst Oct 3, 2020
@BrandonSurowiec
Copy link
Contributor

Seems you'd have to inline it for correct behavior:

@extends(View::exists('emails.store.' . $storeId) ? 'emails.store.' . $storeId : 'emails.store.default')

@taylorotwell taylorotwell merged commit 9f0bb04 into laravel:8.x Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants