Symfony 4: The Twig Recipe

Video loading...

  • 0:03
    Symfony 4: The Twig Recipe with Ryan Weaver
  • 0:08
    Do you remember the only rule for a controller?
  • 0:11
    It must return a Symfony response object.
  • 0:15
    But Symfony doesn't care, how you do that.
  • 0:18
    You could render a template,
  • 0:19
    make API requests
  • 0:21
    or make database queries and build a JsonResponse.
  • 0:25
    Really, most of learning Symfony involves learning to install
  • 0:29
    and use a bunch of powerful
  • 0:31
    but optional tools that make this work easier.
  • 0:36
    If your app needs to return HTML,
  • 0:38
    then 1 of these great tools is called Twig.
  • 0:42
    First, make sure you commit all of your changes so far.
  • 0:46
    I already did this.
  • 0:47
    Recipes are so much more fun when you can see what they do.
  • 0:53
    Now, run composer require twig.
  • 0:56
    By the way, in future tutorials, our app will become a mixture
  • 1:01
    of a traditional HTML app and an API with a JavaScript front end.
  • 1:06
    So if you want to know about building an API in Symfony, we'll get there.
  • 1:10
    This installs twig-bundle,
  • 1:12
    a few other libraries and configures a recipe.
  • 1:18
    What did that recipe do?
  • 1:20
    Let's find out.
  • 1:21
    git status.
  • 1:23
  • 1:24
    Lots of good stuff.
  • 1:26
    The first change is config/bundles.php.
  • 1:31
    Bundles are the plugin system for Symfony.
  • 1:34
    And whenever we install a third-party bundle,
  • 1:37
    Flex adds it here so that it's used automatically.
  • 1:41
    Thanks, Flex.
  • 1:43
    The recipe also created some stuff like a templates directory.
  • 1:48
    Yep, no need to guess where templates go, it's pretty obvious.
  • 1:54
    It even added a base layout file that we'll use soon.
  • 1:58
    Twig also needs some configuration,
  • 2:00
    so the recipe added it in config/packages/twig.yaml.
  • 2:05
    But even though this file was added by Flex, it's yours to modify.
  • 2:11
    You can make whatever changes you want.
  • 2:14
    Oh, and I love this:
  • 2:16
    why do our templates need to live in a templates directory?
  • 2:20
    Is that hard-coded deep inside Symfony?
  • 2:23
    Nope, it's right here.
  • 2:25
    Don't worry about this percent syntax yet;
  • 2:29
    you'll learn about that in a future episode.
  • 2:31
    But, you can probably guess what's going on,
  • 2:35
    %kernel.project_dir% is a variable
  • 2:38
    that points to the root of the project.
  • 2:41
    Anyways, looking at what a recipe did is a great way to learn.
  • 2:46
    But the main lesson of Flex is this:
  • 2:48
    install a library and it takes care of the rest.
  • 2:52
    Now, let's go use Twig.

Symfony 4: The Twig Recipe


In this tutorial, we'll install a Twig bundle and recipe.

Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.