Skip to content

Conversation

@daniel-werner
Copy link

Hi,

I had a situation where a div with id="app" already existed where I wanted to use the @inertia directive.
I solved it by changing the other div, but I thought it would be good to be able to pass the id as an optional argument to the directive like @interia('div-id')

This PR implements the above mentioned feature, and adds a test to it.

Please let me know what do you think.

@claudiodekker claudiodekker requested a review from reinink October 29, 2020 21:50
@claudiodekker
Copy link
Contributor

claudiodekker commented Oct 29, 2020

@daniel-werner Did you test this manually as well? Apparently we had this feature before (#139), which caused #140.

Admittedly, that one didn't have any tests, but the implementation itself looks very similar?
I tried fixing this in #141 a while ago, and while it does fully work, the tests are a bit hairy.

@daniel-werner
Copy link
Author

@claudiodekker Yes, I just tested this manually and it works.

However during the implementation and testing I also encountered the same issue from #140.
It is a bit tricky, the reason of this problem is how blade handles custom directives (this code is from the blade compiler):

    protected function callCustomDirective($name, $value)
    {
        if (Str::startsWith($value, '(') && Str::endsWith($value, ')')) {
            $value = Str::substr($value, 1, -1);
        }

        return call_user_func($this->customDirectives[$name], trim($value));
    }

So, when the directive is called without arguments, an empty string is passed to the function, because of the trim($value).

This is why the default argument value ($id = 'app') is not working because it is actually always called with an argument. So checking for an empty value and defaulting to 'app' works.

Hope my explanation makes sense.

@claudiodekker
Copy link
Contributor

claudiodekker commented Oct 30, 2020

Managed to test this, and it indeed works mostly as you described:

  • @inertia
    <div id="app" data-page="...

  • @inertia('test')
    <div id="'test'" data-page="... <-- note the nested quotes!

  • @inertia(test)
    <div id="test" data-page="... <-- working as intended

@reinink
Copy link
Member

reinink commented Oct 30, 2020

Is there anyway we can remove the quotes when doing this?

@inertia('test')

@daniel-werner
Copy link
Author

I think, we could trim the single and double quotes in the directive function

@reinink
Copy link
Member

reinink commented Oct 30, 2020

@daniel-werner Are you able to try and make that happen?

@daniel-werner
Copy link
Author

@reinink Fixed it

@reinink
Copy link
Member

reinink commented Oct 30, 2020

Thanks! Let me do some testing with this. 👍

@claudiodekker
Copy link
Contributor

claudiodekker commented Jan 4, 2022

I'll be closing this PR, as we've actually already re-implemented this as part of #339 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Closed 🚪

Development

Successfully merging this pull request may close these issues.

3 participants