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

Support ability to pass array data to root template #40

Closed
njoguamos opened this issue Jun 15, 2019 · 11 comments
Closed

Support ability to pass array data to root template #40

njoguamos opened this issue Jun 15, 2019 · 11 comments

Comments

@njoguamos
Copy link

njoguamos commented Jun 15, 2019

It would be nice if withViewData() method could accept array. That way one, can pass multiple value.

This throws an error (Too few arguments to function)

public function index() {
    return Inertia::render('Contact/Index')->withViewData([
        'title'       => 'Contact Us | My App',
        'description' => 'Contact us with your comments, questions, and suggestions',
        'meta'        => 'Some other SEO meta'
    ]);
}

Then access the variables in blade as

<title>{{  $title }}</title>
<meta name="description" content="{{  $description }}.">
<meta name="description" content="{{ $meta }}">

Or am I missing something

@nckrtl
Copy link

nckrtl commented Jun 17, 2019

Hi @njoguamos

You can already pass an array, withViewData is not needed.

public function index() {
    return Inertia::render('Contact/Index', [
        'title'       => 'Contact Us | My App',
        'description' => 'Contact us with your comments, questions, and suggestions',
        'meta'        => 'Some other SEO meta'
    ]);
}

If you'll define the props (title, description and meta) in your Index.vue the values will become available.

@njoguamos
Copy link
Author

I believe the idea of @reinink having withViewData is to be able to pass variables to Laravel blade template.

@nckrtl the solution you provided works only if one want to pass data to vue (which by the way is okay) but I want the array data to be accessible to my app.blade.php

@nckrtl
Copy link

nckrtl commented Jun 17, 2019

aha, in that case I misunderstood your question.

@njoguamos
Copy link
Author

😄 Not really, I am the one who did not explain well.

@georgehanson
Copy link
Contributor

@njoguamos I noticed there is a comma after specifying the Vue Component name. The render method on the ResponseFactory has the second argument default to an empty array if it has not been provided, so this exception shouldn't be thrown. Have you tried changing it from this:

public function index() {
    return Inertia::render('Contact/Index',)->withViewData([
        'title'       => 'Contact Us | My App',
        'description' => 'Contact us with your comments, questions, and suggestions',
        'meta'        => 'Some other SEO meta'
    ]);
}

To this:

public function index() {
    return Inertia::render('Contact/Index')->withViewData([
        'title'       => 'Contact Us | My App',
        'description' => 'Contact us with your comments, questions, and suggestions',
        'meta'        => 'Some other SEO meta'
    ]);
}

@njoguamos
Copy link
Author

@georgehanson Thanks for noting that. It was a typo and have updated my comment.

However, I dug deeper to Inertia and found out that withViewData does not work array

    public function withViewData($key, $value)
    {
        $this->viewData[$key] = $value;
        return $this;
    }

Instead it should be as suggested by this PR

    public function withViewData($key, $value = null)
    {
        if (is_array($key)) {
            foreach ($key as $k => $value) {
                $this->viewData[$k] = $value;
            }
        } else {
            $this->viewData[$key] = $value;
        }
        
        return $this;
    }

I hope am clear 😄

@georgehanson
Copy link
Contributor

@njoguamos that makes sense, passing an array would probably be a good feature to have.

@connecteev
Copy link

+1 on this...unless I'm missing a way to update the content in the tag for SEO?

@enzonotario
Copy link

@connecteev check this: https://github.com/inertiajs/inertia-laravel#accessing-data-in-root-template

@connecteev
Copy link

@enzonotario thanks

@njoguamos
Copy link
Author

@reinink has updated withViewData to support array see commit

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

No branches or pull requests

5 participants