template-talphptal

PHPTAL: how to dump an object to see all properties


I have to modify the PHPTAL template below by adding another field, "location"

  <tal:block tal:repeat="contact Model/contactList">
    <div class="contactCell">
      Name: <span content="contact/name">contact name</span><br/>
      Number: <span content="contacy/number">2374687234</span><br/>

      <-- THIS NEEDS ADDING-->
      Location: <span content="contact/location">contact's location</span>

    </div>
  </tal:block>

My problem is that I don't know what methods and properties are available in the model and I don't want to read the PHP code to trace this either. I would like to be able to dump out all the properties of the model from within the template so it's easy to see if the property I need is already available or whether I have to ask the backend dev to make it available. Something like this would be good.

<div class="debug panel">
  <tal:dumpObject Model/contactList>
</div>

which would then produce something like this in my HTML output:

<div class="debug panel">
  contact Array
  [
    {
      [name] => John Smith
      [number] => 374862378
      [address] => 22 Acacia Avenue
      [location] => London
    },{
      [name] => Billy Bragg
      [number] => 384567365
      [address] => 10 Downing Street
      [location] => London
    },
    ...
  ]
</div>

This way I would be able to immediately see what I can use and what I need to request from other devs.


Solution

  • You can get pretty far with:

    <pre tal:content="php:print_r(object, true)"/>
    

    if the object is a plain array or a stdClass object.

    However, PHPTAL can also read object's methods and invokes magic __get() methods, so if the object is from some fancy ORM it may not be possible to list all properties that would work.