Buy Access to Course

Container, Twig: All Smooth

Share this awesome video!


Keep on Learning!

When you want to render a template, you'll usually extend Controller and it'll give you a bunch of shortcuts, including render(). That's a great idea. But I'll extend ContainerAware instead:

// ... lines 1 - 4
use Symfony\Component\DependencyInjection\ContainerAware;
// ... lines 6 - 8
class MightyMouseController extends ContainerAware
// ... lines 11 - 22

That gives access to the service container, but not the shortcut methods. So I can't say $this->render(), but I can say $html = $this->container->get('twig') and then call render() on it. Render mighty_mouse/rescue.html.twig and pass it a quote variable. But that's just HTML - so wrap it in a Response and return:

// ... lines 1 - 8
class MightyMouseController extends ContainerAware
// ... lines 11 - 13
public function rescueAction()
$html = $this->container->get('twig')->render(
array('quote' => 'Here I come to save the day!')
return new Response($html);

Now, where do templates live? Usually, it's app/Resources/views. But we've moved everything up and out of app/. So our path is just Resources/views: add those directories and a mighty_mouse folder inside. Create rescue.html.twig and extend the base template. We don't have a base template yet, but we will soon. In {% block body %} print out the quote:

{% extends 'base.html.twig' %}
{% block body %}
<h1>{{ quote }}</h1>
{% endblock %}

Now add base.html.twig in Resources/views. Let's steal again from the Standard Edition. Find app/Resources/views/base.html.twig, copy it, and paste it in ours:

<!DOCTYPE html>
<meta charset="UTF-8" />
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}{% endblock %}
<link rel="icon" type="image/x-icon" href="{{ asset('favicon.ico') }}" />
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}

Ok, give it a try. Unable to find template "mighty_mouse/rescue.html.twig". Hmm I bet I have a typo. Yep, my directory was called might_mouse. I went a bit too micro on the name there. Fix that and try again. It's alive!


After creating the views/ directory for the first time, you'll need to clear your Symfony cache, or the template won't be found. That's probably a small Symfony bug, that hopefully we can fix!

Now, let's add the web debug toolbar.