Skip to content

[7.x] Add support for default values for the "props" blade tag #32177

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

Merged
merged 2 commits into from
Mar 31, 2020
Merged

[7.x] Add support for default values for the "props" blade tag #32177

merged 2 commits into from
Mar 31, 2020

Conversation

Persaeus
Copy link
Contributor

Currently, to make default properties for anonymous components, you have to either upgrade to a full component with a component class, or maybe do something like the following:

<!-- /resources/views/components/alert.blade.php -->

@props(['type', 'message'])
@php
    $type = $type ?? 'info'
@endphp

<div {{ $attributes->merge(['class' => 'alert alert-'.$type]) }}>
    {{ $message }}
</div>

This PR adds support for default property values that you specify in the props tag.

For the previous example, this would look something like the following:

@props(['type' => 'info', 'message'])

This benefits the user, as it's more terse and looks neater (in my opinion).

It should not conflict with how the props tag works currently, as users were not expected to pass associative or mixed arrays as arguments.

As for tests, I could not find any tests related to the props tag, so I'm no sure what I need to do.

We use `array_filter($expression, 'is_string', ARRAY_FILTER_USE_KEY)` to extract properties with string keys, and set the corresponding variable to the default value if unset
@taylorotwell taylorotwell merged commit af6cc0b into laravel:7.x Mar 31, 2020
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.

2 participants