0

In php I can use in my template:

use My\WebBundle\Classes\Util;

How can I put the same in Twig tempate?

Thanks.

2 Answers 2

3

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 }}
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Victor, is there anywhere I can read about that use command the OP is talking about? I can't seem to find it anywhere in the PHP docs?
Hey @JomarSevillejo , take a look at PHP Namespaces in 120 Seconds free course by KnpUniversity - there will be an answer ;)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.