2017-11-22 2 views
0

나는 나뭇 가지 템플릿의 새로운 생성 나뭇 가지 확장을 사용하는 방법과 문제에 직면하고있어,하지만 난 작동 얻을하지 않는새로운 나뭇 가지 확장

는 위트를 설명 할 수 있습니다 이유를 이해하지 않습니다를 사용하는 방법 그래서 예를

class MyTwigExtension extends \Twig_Extension 
{ 

    public function getTests(){ 
     return array('instanceof' => new \Twig_Test_Method($this, 'isInstanceof'), 
        'appenmantenimiento' => new \Twig_Test_Method($this, 'appEnMantenimiento') 
        ); 
    } 


    public function isInstanceof($var, $clase) { 
     $class = "AppBundle\\Entity\\".$clase; 
     return $var instanceof $class; 
    } 

    public function appEnMantenimiento(){ 
     check the DataBase.... 
     return TRUE or FALSE; 
    } 

    public function getName(){ 
     return "mytwig_extension"; 
    } 

} 

, 나는 appEnMantenimiento 기능을 사용하는 방법을, 나는

{% if entity is instanceof('MyClass') %} {# WORKS FINE #} 
{% if appenmantenimiento() %} OK {% endif %} {# DOESN'T WORK #} 
{% if true is appenmantenimiento() %} OK {% endif %} {# WORKS fine with the returned value form appenmantenimiento() #} 

의미하지만 문제는 내가 이해하지 않는 이유

+3

을 당신은'functions'와'tests' 혼합된다. [documentation] (https://twig.symfony.com/doc/2.x/advanced.html#id2) – DarkBee

+1

을보십시오. https://stackoverflow.com/questions/10788138/instanceof -operator-in-twig-symfony-2 – COil

답변

1

getFunctions 메서드를 추가하려고 했습니까? 자세한 내용은 참조하십시오 https://twig.symfony.com/doc/2.x/advanced.html#id2

public function getFunctions() 
{ 
    return array(
     new \Twig_SimpleFunction('instanceof', array($this, 'isInstanceof')), 
     new \Twig_SimpleFunction('appenmantenimiento', array($this, 'appEnMantenimiento')) 
    ); 
}