设置属性取决于cookie值 - 良好实践,Laravel 5.2

时间:2016-08-13 21:11:50

标签: php laravel cookies request

我有这个代码在页面加载时触发jQuery slideUp一些元素:

@if(request()->cookie('pin-toolbox-for-objects') == 1)
    <?php $show_toolbox_objects = ''; ?>
@else
    <?php $show_toolbox_objects = 'data-more-close';?>
@endif


<div {{$show_toolbox_objects}} id="toolbox">

    @include('navs._toolbox_object_buttons')

</div>

要做

有没有办法改进此代码?

1 个答案:

答案 0 :(得分:0)

这会更优雅:

<div <?php (request()->cookie('pin-toolbox-for-objects') == 1)?'test':'data-more-close'?> id="toolbox">

但它没有工作。令人惊讶的是,这个确实如此:

<div @if(request()->cookie('pin-toolbox-for-objects') == 1) data-more-close @endif id="toolbox">