Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Twig "is defined" work with iterable objects?

Tags:

php

twig

silex

I have a template that will always receive an iterable object so I can iterate over it. Within the loop the object in each 'result' may or may not have the property I need to display an image, so I have been attempting to use 'defined':. For example:

{% for result in results %}
    {% if result.thumbnail is defined %}
    <img src='{{ result.thumbnail }}' >
    {% endif %}
{% endfor %}

But when I run this I always get the same error from Twig:

Method 'thumbnail' is not implemented

I thought that the 'defined' method would take care of this for me. What am I missing?

I am using Twig (1.18) and Silex (~1.2).

Thank you, Russell

Update

Here is the output of {{ dump(result) }} when the iterator has two objects in it:

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

As can be seen the first object does not have 'thumbnail' but the second one does. Twig is blowing up on the first one.

Update 2

To give a more complete picture the code I am now running has the updated syntax as suggested by @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 %}

Unfortunately this does not solve the problem and I get the same error as before. This time in full:

Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Method 'thumbnail' not implemented") in "search_results" at line 4. (uncaught exception) at /usr/local/turtle/lib/vendor/twig/twig/lib/Twig/Template.php line 304 {"exception":"[object] (Twig_Error_Runtime: An exception has been thrown during the rendering of a template (\"Method 'thumbnail' not implemented\") in \"search_results\" at line 4. at /usr/local/turtle/lib/vendor/twig/twig/lib/Twig/Template.php:304

I would have thought that the 'defined' would check for the existence of the property, maybe using property_exists or something, so that the error would not be thrown when doing the check.

Update 3

I apologise to two commenters, @prisoner and @Alan Tiemblo, I do have magic setter methods. I had forgotten that both the classes that have been set in the iterator extend a 'Model' class that has __get and __set methods in it.

Would this have a bearing on what I am seeing? Is it something like the parent class is not available to Twig for some reason?

like image 527
Russell Seymour Avatar asked Sep 28 '22 17:09

Russell Seymour


1 Answers

To handle this case, I'm used to use the default filter:

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

Edit:

Magic getters are not seen by Twig's property accessor (see \Twig_Template::getAttribute method).

Instead, litterally call your magic getter:

{% if result.__get('thumbnail')|default('') != '' %}
  {# ... #}
{% endif %}
like image 190
Alain Tiemblo Avatar answered Oct 03 '22 00:10

Alain Tiemblo