0

I'm relatively new to Symfony, and currently using v 2.8. I've been using the @Template annotation successfully like this:

/**
 * @Route("/editleague/")
 * @Template()
 */
public function editAction() {
  return $array;
}

And that successfully renders the twig template at Bundle/Resources/views/Default/edit.html.twig

I decided I want a different response (not a Twig template) if it was a post request, so just to start off, I changed the above code to:

/**
 * @Route("/editleague/")
 */
public function editAction() {
  return $this->render("Default/edit.html.twig",$array);
}

But I get a 500 error. I've tried various combinations, but haven't gotten anything to work where I can control the rendered template in the function itself. I believe it's a simple issue that someone with more experience will be able to figure out in seconds.

3
  • In your example, the variable $array is not defined (but that's probably due to a bad example in your question, right?). So please show what the log of your application is telling you about the error and/or visit it in the dev environment and tell us what is shown there. Commented Dec 30, 2015 at 18:36
  • Yeah, $array is defined, I just left that part out to focus on the parts that changed between the two. I can't believe I forgot to check the development mode. When I do that, it says: Unable to find template "Default/edit.html.twig". Wha would be the location then, i followed (I think) the same idea as listed on the symfony page. Commented Dec 30, 2015 at 18:43
  • Ah, I understand. Just take a look at @Paulpro's answer then. Commented Dec 30, 2015 at 18:48

1 Answer 1

1

Since your template is in your bundle's Resources directory, you should use the Symfony logical name in your call to render:

'AppBundle:Default:edit.html.twig'

or:

':Default:edit.html.twig'

See the difference between paths to templates stored in a bundle versus templates in your app directory.

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

2 Comments

To provide some additional context: The Symfony Best Practice is store templates under app/Resources/views. So when the documentation uses something like render('Default/edit.html.twig'), the referenced template will be searched for in app/Resources/views/Default/edit.html.twig. However, when you store your templates in your own bundle, you need to use the namespaced syntax given by @Paulpro. So AppBundle:Default:edit.html.twig translates to `src/AppBundle/Resources/views/Default/edit.html.twig (the actual location might differ depending on where your bundle is located).
Ok... it looks like this fixed it for me and I better understand template locations. Thanks! Changing "Default/edit.html.twig" to "Default/edit.html.twig" to "Bundle::Default/edit.html.twig" did it.

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.