dongpan1365 2016-08-23 05:45
浏览 98

Laravel和@if语句在javascript对象文字中

I hope i wrote it correctly, as i remember when you have an array options in javascript these are called object literals. I have a problem i can't find how to use @if statement in object literal. I have a small site and everything is located inside welcome.blade.php template so i don't have separate javascript file, the function that i use is just at the bottom of the template inside tags. So here is the whole function that i have

    <script>
        var employees = [
            @foreach ($workers->chunk(20) as $chunk)
            [
                @foreach ($chunk as $worker)
                    {
                        innerHTML:'<div class="container-fluid full-height"><div class="row full-height"><div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 full-height"><div class="person-info"><blockquote><h1>{{ $worker->first_name }} {{ $worker->last_name }}</h1>{{{ $worker->function !== '') ? <footer>{{ $worker->function }}</footer> : '' }}}</blockquote></div></div><div class="col-xs-5 col-sm-5 col-md-5 col-lg-5 col-lg-offset-1 col-md-offset-1 col-sm-offset-1 full-height"><div class="person-photo"><img class="img-responsive" src="thumbs/{{ $worker->image }}" /></div></div></div></div>',
                        src:"thumbs/{{ $worker->thumbnail }}"
                    },
                @endforeach
            ],
            @endforeach
        ]
    </script>

As you can see i need to get all the info from $workers inside innerHTML and that should be output as html content, but as some fields will be empty in database so i need to write @if statement if those fields are not empty to display some code

{{{ $worker->function !== '') ? '<footer>{{ $worker->function }}</footer>' :  '' }}}

But i can't get it how to get the if statement inside innerHTML content to work.

EDIT: The closest that i got it working is like this:

'{{ $worker->function ? "<footer>  $worker->function  </footer>" : "" }}' +

But this just renders the output completely with <footer> so now i have like

John Doe <footer> Quality manager </footer>

  • 写回答

1条回答 默认 最新

  • dongquqiao2010 2016-08-23 05:59
    关注

    try like this

    <script>
    var employees = [
        @foreach ($workers->chunk(20) as $chunk)
        [
            @foreach ($chunk as $worker)
                {
                    innerHTML:'<div class="container-fluid full-height">'
                                 +'<div class="row full-height">'
                                     +'<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 full-height">'
                                        +'<div class="person-info">'
                                            +'<blockquote>'
                                               +'<h1>' {{ $worker->first_name }} {{ $worker->last_name }} +'</h1>'
                                                  @if($worker->function != '') 
                                                    +'<footer>' {{ $worker->function }} +'</footer>'
                                                  @endif
                                            +'</blockquote>'
                                        +'</div>'
                                    +'</div>'
                                   +'<div class="col-xs-5 col-sm-5 col-md-5 col-lg-5 col-lg-offset-1 col-md-offset-1 col-sm-offset-1 full-height">'
                                       +'<div class="person-photo">'
                                          +'<img class="img-responsive" src="thumbs/{{ $worker->image }}" />'
                                       +'</div>'
                                   +'</div>'
                                 +'</div>'
                              +'</div>',
                    src:"thumbs/{{ $worker->thumbnail }}"
                },
            @endforeach
        ],
        @endforeach
    ]
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题