symfony - Call function with parameters from Twig -


apologies if has been asked. i've done searching , not found simple solution.

i have symfony/doctrine project. appropriate way call function twig given business logic should not placed in view.

i have array of objects , check if logged in user owner of each object in post.

for example appropriate

    //in twig template     {% post in posts %}         {% if  post.isowner(user_id) %}             //do stuff         {% endif %}     {% endfor %} 

and in post entity have this

    //in entity         public function isowner(user_id){         if (post.getid() == user_id)             return true;         return false     } 

if isn't best practice, how can achieved appropriately.

dont know u're attempting do, if want loop through specific user posts, you'd rather build bi-directionnal relations , loop on posts using

{% post in app.user.posts %} 

in other hand, if want loop through posts , custom logic posts owned authed user, you're go original code. change things though :

for semantic/readability reasons, change

//in twig template {% post in posts %}     {% if  post.isowner(user_id) %}         //do stuff     {% endif %} {% endfor %} 

to

//in twig template {% post in posts %}     {% if  app.user.isowner(post) %}         //do stuff     {% endif %} {% endfor %} 

or

{% post in posts %}     {% if  post.isownedby(app.user) %}         //do stuff     {% endif %} {% endfor %} 

and correct logic method :

//in entity acmebundle\user     public function isowner(acmebundle\post $post){     if ($this == $post->getuser()) {         return true;     }      return false; } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -