1

How do I set a default value to a variable in a blade partial?

So for example I have 2 variables:

  • softDelete
  • delete

witch I want by default to be true.

@if($softDelete)
    <span data-toggle="tooltip" title="Publish">
        <a href="#delete-{{ $id }}" class="btn btn-default btn-xs btn-flat">
            <i class="fa fa-check"></i>
        </a>
    </span>
@endif

@if($delete)
    <span data-toggle="tooltip" title="Delete">
        <a href="#delete-{{ $id }}" class="btn btn-danger btn-xs btn-delete btn-flat details" data-toggle="modal" data-target="#delete_modal">
            <i class="fa fa-trash"></i>
        </a>
    </span>
@endif

And when I send the view from controller to change the value for some of the variables, depending the case I need.

return view('platform.pages.blocks.property_table_actions',
    [
        'id' => $model->id,
        'delete' => false
    ]);

I tried with a provider:

public function composeActionButtons()
{
    view()->composer('platform.pages.blocks.property_table_actions', function ($view) {
        $view
            ->with('softDelete', true)
            ->with('delete', true)
            ->with('view', true)
            ->with('edit', true);
    });
}

but override the false values from controller.

3
  • 2
    You can use blades ternary shortcut {{ $delete or true }} Commented Jun 20, 2016 at 11:57
  • Undefined variable: delete . Commented Jun 20, 2016 at 12:10
  • @IonVasile Then your statement "I have 2 variables" is incorrect, at least in the view's scope. Commented Jun 20, 2016 at 15:50

4 Answers 4

5

Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. Basically, you want to do this:

{{ isset($name) ? $name : 'Default' }}

However, instead of writing a ternary statement, Blade allows you to use the following convenient short-cut:

{{ $name or 'Default' }}


from the laravel docs.. it should check with isset if the variable exists.. if not a default value is shown

Sign up to request clarification or add additional context in comments.

7 Comments

The logic is not good: @if(isset($edit) or true) <span data-toggle="tooltip" title="Edit"> </span> @endif will always be true. There must be a way by default to set all variables to true, and when I render a view to set false only the button I need.
to be clear you want all variables default on true, only if you override it in the controller as false right?
Yes, this is what I want.
Then you can solve it via the blade default variable.. or this pastebin.com/0xTYHSMt
With the code in pastebin the default will be false for all variables, and I need true.
|
0

I think you're approaching this task in the wrong way - personally. But if you insist, you could also just include a partial (I do this sometimes). Say in a variable, etc.

For example, something like this:

// in your view
@include('some.directory.for.partials', ['softDelete' => 'false']) // you can change this to any value

// partial can then run something like this:
@if(count($softDelete) || $softDelete)
    ...
@else
    ...
@endif

That said, I think you could probably just assign the variable in your controller when you're rendering your view. You're assigning a variable anyways. Why make things complicated with a view composer - when that logic could be in your controller? Just set 'delete' => false to true or what have you for each.

5 Comments

I would like to know the best practice way. How would you approach? To be clear I want all variables default on true, only if I override it in the controller as false.
I would define the variable already in the controller before the view is rendered. You're already doing that in your return view(... anyways. Define the variable there. Don't make things complicated. :)
Yes but I have 20 crud controllers, and for each of them I have or not have some of that buttons. And by default I want to have all variables true. In the question I gave 2 buttons as example but in my case I have 5. So if I follow your example I would have to write for each of this buttons if true or false. And my intention was to declare false if only is neede.
Then just use my example. You don't need the view composer (the problem with a view composer is the variables will be added last and will override your controller ones. So think of this logic backwards and reverse the process). Create a partial for those buttons. They're all visible by default, based on an @if conditional in the partial. If one is FALSE, enter variable in the @include method like in my example.
0

I found the best method to be this:

<?php

 namespace App\Providers;

 use Illuminate\Support\Facades\Log;
 use Illuminate\Support\Facades\View;
 use Illuminate\Support\ServiceProvider;

 class ViewComposerServiceProvider extends ServiceProvider
{
/**
 * Bootstrap the application services.
 *
 * @return void
 */
public function boot()
{
    $this->composeActionButtons();
}

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    //
}

/**
 * Compose the Recent sidebar
 */
public function composeActionButtons()
{
    view()->creator('platform.pages.blocks.property_table_actions', function ($view) {
        $view
            ->with('softDelete', true)
            ->with('delete', true)
            ->with('view', true)
            ->with('edit', true);
    });

}
}

and in controller:

  return view('platform.pages.blocks.property_table_actions',
    [
        'id' => $model->id,
    ])
    ->with(['edit' =>false, 'softDelete' => false]);

View creators work almost exactly like view composers; however, they are fired immediately when the view is instantiated. The callback that I defined with the Creator is called immediately after view() , which is before any ->with()s. This enables me to set a default value for the View and then override it when the View is called, if desired.

3 Comments

Looks good - if it works, it works. :) Not,e you don't need to use with twice like that it can accept arrays. So ->with(['edit' =>false, 'softDelete' => false]);
Ohh, Good to know. :)
-1

In Controller.php

function __construct() {
    $softDelete= true;
    $data = array('softDelete'  => $softDelete);
    view()->share($data);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.