laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.65k stars 11.04k forks source link

Getting error using condition directives on blade components. #32045

Closed mojtabaahn closed 4 years ago

mojtabaahn commented 4 years ago

Description:

Imagine we have blade component component-a and we want to pass some attribute to it on some condition ( well, if condition did not met, we want to pass nothing ).

Currently when using condition directives ( @if, @isset , ...) it will make error : syntax error, unexpected 'endif' (T_ENDIF), expecting end of file

Steps To Reproduce:

<!--component-a.blade.php-->
<div {!! $attributes !!}></div>
<!-- main view file -->
<x-component-a @if(true) property @endif />
driesvints commented 4 years ago

I see you're using 7.0.2, can you upgrade to the very latest version? Numerous fixes have been made since.

mojtabaahn commented 4 years ago

Thanks for feedback. Upgraded to 7.2.1 and same results appear.

driesvints commented 4 years ago

Unfortunately we don't seem to support this syntax. Please try:

@if(true)
<x-component-a property />
@else
<x-component-a />
@endif
stuartcusackie commented 3 years ago

Unfortunately we don't seem to support this syntax. Please try:

@if(true)
<x-component-a property />
@else
<x-component-a />
@endif

My card component expects a lot of markup in its $slot. It seems I would have to write a lot of code twice with this solution.

nagi1 commented 3 years ago

I hate to say this without providing ant solutions or suggestions, but I have the same problem. Is there any other elegant way to handle this?

nagi1 commented 3 years ago

I hate to say this without providing ant solutions or suggestions, but I have the same problem. Is there any other elegant way to handle this?

15 min later, I solved this (in my usecase)

input-component.blade.php

        <input type="checkbox" id="{{ $name }}" name="{{ $name }}" {{ $attributes->merge(['class' => $makeItemClass($errors->first($name))]) }}
 @if($isChecked) checked @endif <--- added a flag here
/>

InputComponent.php

class InputSwitch extends InputGroupComponent
{
    public $isChecked;

    public function __construct( $isChecked = false) {
        $this->isChecked = $isChecked;
    }
}

Usage in my view

                <x-admin.input-switch name="is_active" label="{{ __('Active') }}" label-class="text-dark" top-class="col-2" :is-checked="true">
                </x-admin.input-switch>

Cheers :wine_glass:

driesvints commented 3 years ago

No there isn't unfortunately. This is pretty hard to make possible with the compiler. If anyone can figure out on how to make directives in components possible we're welcoming PRs.