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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add View::either method #20695

Merged
merged 1 commit into from
Aug 25, 2017
Merged

[5.5] Add View::either method #20695

merged 1 commit into from
Aug 25, 2017

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.

None yet

3 participants