PHP   发布时间:2022-04-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – Twig“已定义”是否与可迭代对象一起使用?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个模板,它总是会收到一个可迭代的对象,所以我可以迭代它.在循环内,每个’结果’中的对象可能有也可能没有我需要显示图像的属性,所以我一直在尝试使用’defined’:例如:
{% for result in results %}
    {% if result.thumbnail is defined %}
    <img src='{{ result.thumbnail }}' >
    {% endif %}
{% endfor %}

但是当我运行这个时,我总是从Twig得到同样的错误:

我认为’定义’方法会为我解决这个问题.我错过了什么?

我正在使用Twig(1.18)和Silex(~1.2).

谢谢拉塞尔

更新

当迭代器中有两个对象时,这是{{dump(result)}}的输出:

object(Turtle\Model\Attachment)[1056]
  protected 'datatype' => string 'Attachments' (length=11)
  protected 'filename' => string '560166.jpg' (length=10)
  protected 'displayname' => null
  protected 'description' => null
  protected 'collection' => string 'product' (length=7)
  protected 'width' => int 360
  protected 'height' => int 360
  protected 'file' => string '/9j/4QP+RXhpZgAASUkqAAgAAAAYAAABAwABAAAA8AAAAAEBAwABAAAAtAAAAAIBAwADAAAALgEAAAYBAwABAAAAAgAAAA4BAgAgAAAANAEAAA8BAgAFAAAAVAEAABABAgAJAAAAWQEAABIBAwABAAAAAQAAABUBAwABAAAAAwAAABoBBQABAAAAYgEAABsBBQABAAAAagEAACgBAwABAAAAAgAAADEBAgAeAAAAcgEAADIBAgAUAAAAkAEAABMCAwABAAAAAgAAAAGkAwABAAAAAAAAAAKkAwABAAAAAAAAAAOkAwABAAAAAAAAAAakAwABAAAAAAAAAAikAwABAAAAAAAAAAmkAwABAAAAAAAAAAqkAwABAAAAAAAAAKXEBwAcAAAApAEAAGmHBAABAAAAwAEAAJgDAAAIAAgACAAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgAFNPTlkARFNDLVAyMDAAgPwKABAnAACA/AoAECcAAEFk'... (length=34432)
  protected 'mimetype' => string 'image/jpeg' (length=10)
  protected 'subdir' => string 'products/acme' (length=16)
  protected 'admupdated' => null
  protected 'website' => string 'gaspares' (length=8)
  protected 'contentids' => null
  protected 'category' => null
  protected 'id' => string 'pIPp0QP_TYqA0JuWmPGBmw' (length=22)
  protected 'app' => null
  protected 'entity' => null
  protected 'item' => null
  protected 'namefield' => string 'filename' (length=8)
  protected 'filterfield' => null
  protected 'progress' => 
    array (size=0)
      empty
  public 'checksum' => string 'c3d3f0194b988297e603367d5fd3837b' (length=32)

object(Turtle\Model\Product)[1055]
  protected 'datatype' => string 'Products' (length=8)
  protected 'state' => null
  protected 'manufacturer' => string '****' (length=7)
  protected 'code' => string '560166' (length=6)
  protected 'ghost' => boolean false
  protected 'discontinued' => boolean false
  protected 'title' => string '****' (length=39)
  protected 'summary' => string '****' (length=43)
  protected 'description' => string '****' (length=407)
  protected 'information' => string '****' (length=257)
  protected 'delivery' => string 'Next Working Day,On A' (length=22)
  protected 'delivery_type' => string 'On A UPS Before 12.30 Service' (length=29)
  protected 'tagname' => null
  protected 'tag' => 
    array (size=7)
      0 => string 'AR4' (length=3)
      1 => string 'AR3' (length=3)
      2 => string 'AR2' (length=3)
      3 => string 'AR5 ' (length=4)
      4 => string 'AR7' (length=3)
      5 => string 'AR6' (length=3)
      6 => string 'AR1' (length=3)
  protected 'warranty' => string '1 Year' (length=6)
  protected 'catid' => null
  protected 'cost' => string '74.46' (length=5)
  protected 'price' => null
  protected 'image' => string 'products/acme/560166.jpg' (length=27)
  protected 'thumbnail' => string 'products/acme/thumb560166.jpg' (length=32)
  protected 'techinfo' => string 'notechinfo.jpg' (length=14)
  protected 'checksum' => string '4db345d582ac77dcd5594dc75bc2726d' (length=32)
  protected 'lastmodified' => int 1424208468
  protected 'admupdated' => null
  protected 'id' => string 'IF8B94PuRieIsHWARfI51w' (length=22)
  protected 'app' => null
  protected 'entity' => null
  protected 'item' => null
  protected 'namefield' => string 'code' (length=4)
  protected 'filterfield' => null
  protected 'progress' => 
    array (size=0)
      empty

可以看出,第一个对象没有“缩略图”,但第二个对象没有. Twig正在爆炸第一个.

更新2

为了给出更完整的图片,我现在运行的代码具有@prisoner建议的更新语法:

{% for result in results %}
<div class='row'>
  <div class='col-md-1 col-md-offset-1'>
    {% if result.thumbnail is defined and result.thumbnail is not empty %}
    <a href="{{ app.url_generator.generate('product_show',{'id': result.id}) }}">
      <img src='/images/{{ result.thumbnail }}' align='left' height='75'>
    </a>
    {% endif %}
  </div>
</div>
{% endfor %}

不幸的是,这并没有解决问题,我得到了与以前相同的错误.这次完整:

我原本认为’defined’会检查属性是否存在,可能使用property_exists或其他东西,这样在进行检查时就不会抛出错误.

更新3

我向两位评论者道歉,@prison和@Alan Tiemblo,我确实有魔术制定方法.我忘记了在迭代器中设置的两个类都扩展了一个’Model’类,其中包含__get和__set方法.

这会对我看到的情况产生影响吗?是不是因为某些原因,Twig无法使用父类?

解决方法

为了处理这种情况,我习惯使用 default过滤器:
{% if result.thumbnail|default('') != '' %}
  {# ... #}
{% endif %}

编辑:

Twig的属性访问者看不到魔术吸气剂(见\Twig_Template::getAttribute方法).

相反,小心翼翼地称呼你的魔法吸气剂:

{% if result.__get('thumbnail')|default('') != '' %}
  {# ... #}
{% endif %}

大佬总结

以上是大佬教程为你收集整理的php – Twig“已定义”是否与可迭代对象一起使用?全部内容,希望文章能够帮你解决php – Twig“已定义”是否与可迭代对象一起使用?所遇到的程序开发问题。

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

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