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

Conversation

Projects
None yet
3 participants
@sebastiandedeyne
Contributor

sebastiandedeyne commented Aug 23, 2017

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

This comment has been minimized.

Show comment
Hide comment
@lucasmichot

lucasmichot Aug 23, 2017

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.');
}
Contributor

lucasmichot commented Aug 23, 2017

@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

This comment has been minimized.

Show comment
Hide comment
@taylorotwell

taylorotwell Aug 23, 2017

Member

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

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

2 checks passed

continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@taylorotwell

This comment has been minimized.

Show comment
Hide comment
@taylorotwell

taylorotwell Aug 25, 2017

Member

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

Member

taylorotwell commented Aug 25, 2017

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