0

I have problem with path to twig file. I would like to shorten the path. Look at my example:

class DefaultController extends Controller
{
    public function indexAction($name)
    {
        return $this->render('CatalogFrontendBundle:Default:index.html.twig', array('name' => $name));
    }
}

Of course it will work, but look at the path to twig file it's so long.. I now that I could use annotation like this @Template() but I don't want to. Is there any other way to use default twig file to render my page?

Default twig file - I understand as the file whose name is the same like name of action method. So if name of action is indexAction the name of twig file should be index.html.twig.

6
  • As you said, you don't want to use @Template(), but it's made for what you want... Is there any reason that you want to invent again the wheel ? Commented Apr 10, 2014 at 13:52
  • Heh this is good question, you see this is my conception to dont make annotation. I read that they are slow. I don't like routing annotation so I decided to don't use it. And because of it I like to find the way how to made something similar to @Template() Commented Apr 10, 2014 at 13:57
  • "I read that they are slow", in some cases. @Template will not slow down your app much Commented Apr 10, 2014 at 13:59
  • 2
    @Template is not slow. All of the different methods for routing, templating etc.. are all parsed into php cache files on the first request, so apart from that one request there is no performance difference. Commented Apr 10, 2014 at 14:07
  • 1
    It sounds like you are taking issue with Symfony2 philosophy. If you are concerned about speed and terse code above everything, Symfony2 is not the right framework for your needs. Symfony1 followed the RAD philosophy of Ruby on Rails. Symfony2 got rid of this preference toward magical conventions, and is based largely on Java frameworks like Spring which favor explicit and readable code. Commented Apr 10, 2014 at 14:10

2 Answers 2

2

If you'd like a Symfony experience that is more geared toward Rapid Application Development (RAD) take a look at the KNP RAD bundle. This gives you a lot of convention-based shortcuts to work with, including automatic template resolution. See here: http://rad.knplabs.com/#template

Sign up to request clarification or add additional context in comments.

Comments

-1

I can't found better solution to my problem. So I decide to use @Template, Mark have right it will be cached. And I think that better option is to use those annotation than write my own method or put all of the path in the render method.

So the answer is below:

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

    class DefaultController extends Controller
    {
        /**
         * 
         * @Template()
         */
        public function indexAction($name)
        {
            return array('name' => $name);
        }
    }

1 Comment

This is not correct. you can simply return an array if you use the @Template() annotation. so return ['name'=>$name];

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.