Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig instanceof for inheritance objects

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

like image 455
Alistair Prestidge Avatar asked Apr 11 '12 20:04

Alistair Prestidge


4 Answers

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 %}
like image 107
KingCrunch Avatar answered Oct 23 '22 09:10

KingCrunch


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 %}
like image 27
Kristian Sandström Avatar answered Oct 23 '22 08:10

Kristian Sandström


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.

like image 45
Christian Riesen Avatar answered Oct 23 '22 10:10

Christian Riesen


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 %}
like image 35
Nico Avatar answered Oct 23 '22 08:10

Nico