将变量放在刀片的if条件内的语法

时间:2019-08-02 08:03:49

标签: php laravel

如果要满足条件,我试图显示包含变量的HTML,否则仅显示“测试”字样。

<a href="/StuffSpot/notifPost/{{ $notification->id }}">{{ $notification->read == 1 ? '<strong>{{$notification->send_uname}}</strong> has commented on <strong>{{$notification->title}}</strong> post' : 'test' }}</a> 

我收到以下错误:

  

解析错误:语法错误,意外的'}',期望为','或')'

1 个答案:

答案 0 :(得分:1)

<a href="/StuffSpot/notifPost/{{ $notification->id }}">
    @if($notification->read == 1)
        <strong> {{ $notification->send_uname }}</strong> has commented on <strong>{{ $notification->title }}</strong> post
    @else
        test
    @endif
 </a>

尝试使用if else而不是一行三元逻辑。

此外,您已经使用{{ }}进行回显,不要在另一个内部使用{{ }},要使用{!! !!}显示html标签,laravel刀片{{ }}会逃逸并回显清除这些字段中的html标签