PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-在树枝三元中定义大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

{{item.nw已定义,而item.nw是? ‘target =“ _ blank”’}}在下面的脚本中.有更简洁的方法吗?

我以{{item.nw? ‘target =“ _ blank”’}}可能有效,但如果item.nw不为false,则返回item.nw;如果item.nw为false,则返回target =“ _ blank”'(参https://twig.symfony.com/doc/2.x/templates.html#test-operator)

{% macro menu(menu,activE) %}
{# menu is an associated array of containing:
name.  required
path or id: One of the two are required.  If both, URL will use path
path.  optional and defaults to javascript:void(0)
id.  optional and defaults to not adding an id to the item.
nw.  optional and defalts to false.  This is a flag for a new window.
class.  optional and defaults to not adding an class to the item.
#}
{% for item in menu %}
{% set path = item.path is defined?item.path:"javascript:void(0)" %}
{% set id = item.id is defined?item.id:null %}
{% set class = item.class is defined?item.class:"" %}
{% if (path == active or id == activE) %}
{% set class = class~' active ' %}
{% endif%}
{% if loop.first %}
{% set class = class~' first ' %}
{% elseif loop.last %}
{% set class = class~' last ' %}
{% endif %}
<li class="{{ class|trim }}">
    <a href="{{ path }}"{{ item.nw is defined and item.nw ? ' target="_blank"' }}{{ id?"id=#{iD}" }}>{{item.namE}}</a>
</li>
{% endfor %}
{% endmacro %}

解决方法:

如果“已定义并且不为空”,则空合并将仅返回左侧的值.请记住,false也是一个值.

{{ item.nw ?? ' target="_blank"' }}

启用“严格模式”时,应检查在Twig模板中使用的每个变量.它可能很冗长,但比让模板中断更好.

如果使用认过滤器,则可以使用其他方法来检查变量. https://twig.symfony.com/doc/2.x/filters/default.html

{{ item.nw|default() is not false ? ' target="_blank"' }}

{{ item.nw|default() == 'some_value' ? ' target="_blank"' }}

大佬总结

以上是大佬教程为你收集整理的php-在树枝三元中定义全部内容,希望文章能够帮你解决php-在树枝三元中定义所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: