19

I am using the following feature from propel http://www.propelorm.org/documentation/09-inheritance.html.

I am also using Symfony2 and Twig

I have a class structure using the above feature that looks something like this

class Event {}

class Birthday extends Event {}

class Walking extends Event {}

now I pass an event object to a twig template and I want to know what type of event it is

For instance I want to display an image of a cake if its a birthday and I want to display map routes if its walking event.

I cannot use instanceof in Twig as this feature does not exist. Does anyone now why this does not exist? and is there a way I can replicate this functionality without having to do something like

 public function getType()

in each class, or

 public function isBirthday()

in the event class.

I found this on github but it is of no use to me. I have commented on their to see if I can get an answer.

https://github.com/fabpot/Twig/issues/553

j0k
  • 21,914
  • 28
  • 75
  • 84
Alistair Prestidge
  • 659
  • 3
  • 9
  • 19

6 Answers6

53

I share the opinion, that instanceof is nothing that should appear in a template. I use twig-tests for this case

class MyTwigExtension extends TwigExtension
{
    public function getTests ()
    {
        return [
            new \Twig_SimpleTest('birthday', function (Event $event) { return $event instanceof Birthday; }),
            new \Twig_SimpleTest('walking', function (Event $event) { return $event instanceof Walking; })
        ];
    }
}

And in the template

{% if event is birthday %}{# do something #}{% endif %}
KingCrunch
  • 119,075
  • 18
  • 142
  • 167
  • I think I'm just being dense, but I don't seem to be able to find that TwigTest class anywhere in @fabpot/Twig. Have you defined a custom class that extends `\Twig_Test`, or is it an alias to another class? `\Twig_Test` itself is abstract and thus uninstantiatable. – Benjamin Nolan Mar 05 '14 at 15:31
  • 2
    Found it. I'm guessing the class was renamed at some point in the last six months. `\Twig_SimpleTest` works in place of `TwigTest` above. See: http://twig.sensiolabs.org/doc/advanced.html#tests – Benjamin Nolan Mar 05 '14 at 16:20
  • 1
    @TwoWholeWorms Nope, I just like to import them like `use \Twig_Test as TwigTest; // or even "as Test"` (and it was copy-pasted from somewhere). Fits better into the overall coding style – KingCrunch Apr 16 '14 at 21:23
  • I beg to differ, if I have all my associated items and I want to list them on the same page or display a count, why should I filter in my controller and have to pass yet more variables to the view when the view already has these items and just need to be broken out for display purposes? – tlorens Jun 25 '20 at 15:49
6

An indirect way of accomplishing this would be testing the object for a method, if you know each inherited object has a unique method. Maybe your Birthday class has a getBirthday(), while your Walking class has a getMap()?

{% if yourobject.methodName is defined %}
   //Stuff that should only output when the object in question has the requested method
{% endif %}
Veve
  • 6,182
  • 5
  • 37
  • 53
  • There's no magic `.method` getter added by Twig to each object. So as long as `yourobject` doesn't actually have a method named "method" you expression will never become true. Instead simply use `{% if yourObject.methodName is defined %}` where "methodName" is the exact name of the function you're checking for. – flu Oct 08 '13 at 10:08
  • 1
    For those, who want to know: Thats called "Duck-Typing" http://en.wikipedia.org/wiki/Duck_typing :) – KingCrunch Apr 16 '14 at 21:26
4

Using instanceof in a template is frowned upon from an architectual standpoint. If you find yourself in a position where you "need" it, you have probably uncovered a problem in your architecture. Your getType solution in your case is probably the best. You could still put that into the event base class and read it out the name of the implementing class.

Christian Riesen
  • 659
  • 3
  • 10
1

Another solution :

class Event {
    ...
    public function isInstanceOfBirthday() {
        return $this instanceof Birthday;
    }
}

then it will works with any class that inherit from

class Birthday extends Event {}

class Walking extends Event {}

then in your twig :

{{ event.isInstanceOfBirthday ? ... something for Birthday instance ... : ... something for Walking instance ... }}

OR

{% if event.isInstanceOfBirthday %}
    ... do something for Birthday instance ...
{% else %}
    ... do something for Walking instance ...
{% endif %}
Nico
  • 3,151
  • 3
  • 18
  • 25
0

I'm trying to make an index.html.twig that lists entities that are defined by the user, and only the fields that have been marked as 'addToListing' So I get to the point in which I don't know what I'm printing.

{% for entity in entities %}
    <tr>
        {% for heading in headings %}
            <td><a href="{{ path('document_show', { 'id': entity.id, docType: metaData.className }) }}">{{ attribute(entity, heading) }}</a></td>
        {% endfor %}
    </tr>
{% endfor %}

And heading happens to be a \DateTime :/ So for such case I'd need to | date('format') or some better solution.

Any advise on a clean solution for me?

juanmf
  • 1,867
  • 1
  • 24
  • 26
  • Wouldn't the above solution work just fine, since it's a DateTime you know it has the ->format method among other things. If i understand your example something like `if entity.format is defined` should do the trick! – Kristian Sandström Apr 17 '14 at 08:18
  • I, I was worried about the heading type. I replaced the innerHtml by an included template that so far looks like: `{% if bridge.is_scalar(attribute(entity, heading)) or attribute(entity, heading).__toString is defined or attribute(entity, heading) is null %} {{ attribute(entity, heading) ? : 'empty'}} {% else %} {% if bridge.get_class(attribute(entity, heading)) == 'DateTime' %} {#{ dump(bridge.get_class(attribute(entity, heading))) }#} {{ attribute(entity, heading) | date(dateFormat) }} {% else %} {% endif %} {% endif %}` – juanmf Apr 21 '14 at 13:20
  • here is Bridge: `namespace DocDigital\Bundle\DocumentBundle\Helper; /** * Nasty stuff to support needed php fns in twig * * @author Juan Manuel Fernandez */ class TwigPhpBridge { public function __call($functionName, array $argV) { return call_user_func_array($functionName, $argV); } }` – juanmf Apr 21 '14 at 13:22
0

I had similar problem, it was related to the inheritance in Hotel software. I had a base class "RoomEquipment", and inheritance with "Bed", "ElectricAppliances"....

class BaseRoomEquipment {}

class Bed extends BaseRoomEquipment {}

class ElectricAppliances extends BaseRoomEquipment {}

And of course, a class "Room" with relation OneToMany towards RoomEquipment.

On template, I wanted to render beds only, but Room has relation to base equipment, which includes beds and electric appliances.

Instead of testing in twig template, in Room class i have created a method getBeds, and thats it.

class Room {

  private $equipment;

  public getBeds() 
  {
     $res = [];
     foreach($this->getEquipment() as $eq) {
        if ($eq instanceof Bed) $res[] = $eq;
     }
     return $res;
  }

  // Rest of class here....

}

And, of course, in Twig:

<div>
    {% for Bed in Room.beds %}
       {{ Bed.nbPersons }}
    {% endfor %}
</div>

Thanks to Property Accessor component - this is possible. Now, your twig does not have to check about instance type, nor it does no

Nikola Svitlica
  • 464
  • 5
  • 13