Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Block Views & Block Definitions

Video not working?

It looks like your browser may not support the H264 codec. If you're using Linux, try a different browser or try installing the gstreamer0.10-ffmpeg gstreamer0.10-plugins-good packages.

Thanks! This saves us from needing to use Flash or encode videos in multiple formats. And that let's us get back to making more videos :). But as always, please feel free to message us.

Let's create a layout for our "individual recipe" page so that we can customize this a bit more. I love that we can create new layouts on the fly, whenever a page needs to be tweaked.

Adding and Mapping a New Layout

Add a new layout, choose our favorite Layout 2 and call it "Individual Recipe Layout". And y'all know the drill at this point. Start by linking the Header zone... then the Footer zone.

Cool! And then because we're going to be applying this to a normal page that already exists in Twig, add a "Full View" block, which will render the body block from our template.

Solid start. Hit "Publish"... so we can map this. Add a new mapping, link it to the "Individual Recipe Layout"... then hit "Details". This time, let's link via the route name. To get that name, open src/Controller/RecipeController.php. Here it is: app_recipes_show. Paste that, hit "Save Changes" and... let's try this!

We shouldn't see any difference yet and... we don't. But we can see that it's using our layout!

Let's spice this page up a bit! Go back to the layouts admin and edit the "Individual Recipe Layout". Add a new Grid and change it to a "Dynamic collection"... that uses "Contentful search". Load Skills, show the newest first and limit to 3.

Ok, if we "Publish and continue editing"... then refresh... whoa! It's cool that we can just put those anywhere now. Though, let's wrap that in a container. And... much better.

So far, this is all easy! Ready for the complication? I want to customize how this grid looks: I want to have one big recipe on the left and then two smaller recipes on the right. But I do not want to change how the grid looks on other parts of our site, like on the homepage. So the question is: how can we change how this grid renders on just this page?

The Grid/List View Types

Click on the Grid and go to design tab. It turns out that a Grid is really just a "List" block. And the "List" block has two "view types": list and grid.

Head over to your terminal and run:

php ./bin/console debug:config netgen_layouts view.block_view

Oh, but spell netgen correctly. This displays the configuration for how blocks are rendered. Find the default section... then scroll down a bit. Here: we see the two view types for list and grid. As I mentioned, it turns out that these are actually both part of the same block type called list. They're just two different view types: one called list and one called grid. When you switch the "view type" in the layouts admin, you're effectively switching which template is used to render that block.

Block Definitions

Run that same command, but instead of view.block_views, check block_definitions:

php bin/console debug:config netgen_layouts block_definitions

Block definitions is where you define what the blocks actually are. So every root key under this config represents a different block that we can use inside the admin area. Find the one called list: here it is. This defines things like what form fields are rendered in the admin area for this block and what "view types" it has. This has two: list and grid. Layouts reads this config to render the "View Type" select field in the admin. Then, once we select the view type, it uses the block_views config we looked at before to know which template to render.

Ok, enough deep config and theory. Let's give ourselves a new way to render lists by creating a new view type. That's next.

Leave a comment!

0
Login or Register to join the conversation
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.0.0",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "babdev/pagerfanta-bundle": "^3.7", // v3.7.0
        "doctrine/doctrine-bundle": "^2.7", // 2.7.0
        "doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
        "doctrine/orm": "^2.13", // 2.13.3
        "easycorp/easyadmin-bundle": "^4.4", // v4.4.1
        "netgen/layouts-contentful": "^1.3", // 1.3.2
        "netgen/layouts-standard": "^1.3", // 1.3.1
        "pagerfanta/doctrine-orm-adapter": "^3.6",
        "sensio/framework-extra-bundle": "^6.2", // v6.2.8
        "stof/doctrine-extensions-bundle": "^1.7", // v1.7.0
        "symfony/console": "5.4.*", // v5.4.14
        "symfony/dotenv": "5.4.*", // v5.4.5
        "symfony/flex": "^1.17|^2", // v2.2.3
        "symfony/framework-bundle": "5.4.*", // v5.4.14
        "symfony/monolog-bundle": "^3.0", // v3.8.0
        "symfony/proxy-manager-bridge": "5.4.*", // v5.4.6
        "symfony/runtime": "5.4.*", // v5.4.11
        "symfony/security-bundle": "5.4.*", // v5.4.11
        "symfony/twig-bundle": "5.4.*", // v5.4.8
        "symfony/ux-live-component": "^2.x-dev", // 2.x-dev
        "symfony/ux-twig-component": "^2.x-dev", // 2.x-dev
        "symfony/validator": "5.4.*", // v5.4.14
        "symfony/webpack-encore-bundle": "^1.15", // v1.16.0
        "symfony/yaml": "5.4.*", // v5.4.14
        "twig/extra-bundle": "^2.12|^3.0", // v3.4.0
        "twig/twig": "^2.12|^3.0" // v3.4.3
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
        "symfony/debug-bundle": "5.4.*", // v5.4.11
        "symfony/maker-bundle": "^1.47", // v1.47.0
        "symfony/stopwatch": "5.4.*", // v5.4.13
        "symfony/web-profiler-bundle": "5.4.*", // v5.4.14
        "zenstruck/foundry": "^1.22" // v1.22.1
    }
}