user2794692
user2794692

Reputation: 391

How use php "use" function in twig

In php I can use in my template:

use My\WebBundle\Classes\Util;

How can I put the same in Twig tempate?

Thanks.

Upvotes: 0

Views: 240

Answers (2)

Victor Bocharsky
Victor Bocharsky

Reputation: 12306

You can't create objects in twig template, but you can pass them in twig template from controller:

public function postsAction() {

    return $this->render('AcmeBlogBundle:User:posts.html.twig', array(
        'util' => new My\WebBundle\Classes\Util(),
    ));
} 

And in twig template can use it:

{{ util.anyProperty }}

Upvotes: 3

Anyone
Anyone

Reputation: 2835

You don't want PHP in your twig template. All you need to do is tell twig what variables you use. It's part of the MVC.

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Upvotes: 1

Related Questions