【问题标题】:shorthand isset followed by eval简写 isset 后跟 eval
【发布时间】:2020-10-06 18:34:24
【问题描述】:

在 laravel 中设置一些导航链接,并尝试根据 URL 命中设置一些内联检查,这将使导航链接“活动”或不“激活”,但遇到了我无法运行 isset() 之前的问题表达方式。不知道我在哪里不足!

仅供参考 - $third 是 URL 的第三位 - 在这种情况下,URL 仅位于 $second 级别,因此未定义 $third。这意味着我不希望该语句运行。

<a class="nav-link {{ $third ?? $third === 'introduction' ? 'active' : '' }}" href="xyz"

我是怎么跑的

$third === 'introduction' ? 'active' : ''

仅当 $third 存在时!

谢谢

【问题讨论】:

  • 你能解释一下为什么你不能使用isset()
  • 所以喜欢: isset($third ?? ''$third === 'introduction' ? 'active' : '') ---- ?
  • 爆炸:不能对表达式的结果使用 isset()
  • 是的,你为什么要这样使用 isset
  • 你会如何设置它?我想我缺少什么 - 无法弄清楚如何以正确的方式构建它......?

标签: laravel php-7


【解决方案1】:

这不应该工作吗?

<a class="nav-link {{ isset($third) ? ($third === 'introduction' ? 'active' : '') : '' }}" href="xyz">

【讨论】:

  • 我似乎无法弄清楚当 $third 不存在时它不会爆炸的方法......嗯
  • 等待 - 不,对不起,我认为这确实有效 - 我在页面下方有 $third !
【解决方案2】:
<a class="nav-link {{ (isset($second) && isset($third) && $third === 'introduction') ? 'active' : '' }}" href="Xyz">

【讨论】:

    猜你喜欢
    • 2013-09-07
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    • 2011-01-26
    • 2016-11-13
    相关资源
    最近更新 更多