Ali*_*dge 18 propel single-table-inheritance symfony twig
我正在推动http://www.propelorm.org/documentation/09-inheritance.html使用以下功能.
我也在使用Symfony2和Twig
我有一个使用上述功能的类结构,看起来像这样
class Event {}
class Birthday extends Event {}
class Walking extends Event {}
Run Code Online (Sandbox Code Playgroud)
现在我将一个事件对象传递给一个twig模板,我想知道它是什么类型的事件
例如,我希望在生日时显示蛋糕的图像,并且如果它的步行事件我想显示地图路线.
我无法在Twig中使用instanceof,因为此功能不存在.有谁现在为什么这不存在?有没有办法我可以复制这个功能,而不必做类似的事情
public function getType()
Run Code Online (Sandbox Code Playgroud)
在每个班级,或
public function isBirthday()
Run Code Online (Sandbox Code Playgroud)
在事件类中.
我在github上发现了这个,但它对我没用.我评论过他们是否能得到答案.
Kin*_*nch 51
我同意这个观点,这instanceof
不应该出现在模板中.我对这种情况使用了枝条测试
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; })
];
}
}
Run Code Online (Sandbox Code Playgroud)
并在模板中
{% if event is birthday %}{# do something #}{% endif %}
Run Code Online (Sandbox Code Playgroud)
如果您知道每个继承的对象都有一个唯一的方法,那么实现此方法的间接方法是测试方法的对象.也许你的Birthday类有一个getBirthday(),而你的Walking类有一个getMap()?
{% if yourobject.methodName is defined %}
//Stuff that should only output when the object in question has the requested method
{% endif %}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21219 次 |
最近记录: |