Blade Laravel 中的默认变量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37921911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default variable value in blade Laravel
提问by Sebastian Corneliu V?rlan
How do I set a default value to a variable in a blade partial?
如何为刀片部分中的变量设置默认值?
So for example I have 2 variables:
例如,我有 2 个变量:
- 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.
但覆盖来自控制器的错误值。
回答by Alex Meyer
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' }}
{{ isset($name) ? $name : '默认' }}
However, instead of writing a ternary statement, Blade allows you to use the following convenient short-cut:
但是,Blade 允许您使用以下方便的快捷方式,而不是编写三元语句:
{{ $name or 'Default' }}
{{ $name 或“默认”}}
from the laravel docs.. it should check with isset if the variable exists.. if not a default value is shown
来自 laravel 文档 .. 如果变量存在,它应该检查 isset .. 如果不存在,则显示默认值
回答by Mike Barwick
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.
也就是说,我认为您可以在渲染视图时在控制器中分配变量。无论如何,您正在分配一个变量。为什么使用视图作曲家让事情变得复杂 - 当该逻辑可以在您的控制器中时?只需设置'delete' => false
为 true 或您对每个都有什么。
回答by Sebastian Corneliu V?rlan
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.
视图创建者的工作方式几乎与视图作曲家完全一样;然而,当视图被实例化时,它们会立即被触发。我在 Creator 中定义的回调在 view() 之后立即调用,在任何 ->with() 之前。这使我能够为视图设置默认值,然后在调用视图时覆盖它(如果需要)。
回答by Waseem Hussain
In Controller.php
在 Controller.php 中
function __construct() {
$softDelete= true;
$data = array('softDelete' => $softDelete);
view()->share($data);
}