Skip to content

Conversation

sebastiandedeyne
Copy link
Contributor

Adds a View::either method. either returns and instance of the first view if it exists, or the second if it doesn't.

return view()->either("categories.{$category->slug}", 'categories.default', compact('category'));

Wrote this, then realized it didn't solve my current use case, but thought I might as well PR anyway 😅

@lucasmichot
Copy link
Contributor

@sebastiandedeyne, nice ! It makes me feel about template resolutions in Drupal.
Nevertheless, why do you limit to one view and one fallback? Why not providing an array of many views ?

/**
 * @param  array  $views
 * @param  array  $data
 * @param  array  $mergeData
 * @return \Illuminate\Contracts\View\View
 * @throws \InvalidArgumentException
 */
public function either($views, $data = [], $mergeData = [])
{
    foreach ($views as $view) {
        if ($this->exists($view)) {
            return $this->make($view, $data, $mergeData);
        }
    }

    throw new InvalidArgumentException('None of the views could be resolved.');
}

@taylorotwell
Copy link
Member

taylorotwell commented Aug 23, 2017

No plans on adding this. I would just make a helper function in your app that does it.

@taylorotwell taylorotwell reopened this Aug 24, 2017
@taylorotwell taylorotwell merged commit 5dcf584 into laravel:master Aug 25, 2017
@taylorotwell
Copy link
Member

Refactored into View::first and it takes an array of views that can be any length now.

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.

3 participants