Blocks, Layouts, Templates

 

 

Let's look at the routing of each WEB request to Magento. Magento uses the Front controller pattern.Magento first checks the bootstrap file. The bootstrap file is responsible for subsequent loading and initial launch of the application.After this, the Front Controller will be initialized and dispatched.The FrontControler class searches the list of routers provided by the routerList class until it finds one that can handle the request.When the front controller finds a matching router, it sends the request to the action class returned by the router. If the front controller cannot find a router to handle the request, it uses the default router, which will return the configured 404 page by default.When the front controller finds a matching router, it sends a request to the action class returned by the router, then the "execute" method of the controller referred to as the "action" class returned by the router is executed.If you use a "page" or "layout" response object type, that object will execute the "loadLayout" and "renderLayout" methods, and then the response object will return the HTML to the browser.Let's look at the "loadLayout" and "renderLayout" methods in more detail.First, Magento generates a page layout XML. Then it generates blocks according to this XML layout, and then it outputs these blocks.Magento then adds templates to these blocks as per the configuration and then executes the child blocks specified in the template files or XML files and then displays the output.The main components of page design are "layouts", "containers" and "blocks"."Layout" represents the structure of a web page, "containers" represent the placeholders in that page's structure, and "blocks" represent the user interface controls or components within the container placeholders.There are only three different types of view elements in Magento: containers, blocks, and UI components.The page is divided into containers.They are the basic elements of the Magento page structure.Header, footer, left sidebar, main column are the main containers of a Magento website.Blocks, along with containers, are the main components of a Magento page structure.Blocks are some files with templates, based on which HTML code is generated.Side navigation, product list, footer links are some of the most commonly used page blocks. UI component interface extends block interface.Magento UI components are used to present user interface elements such as tables, lists, dialog boxes, and more.They are mainly used in the checkout and admin panel.But how does Magento know which XML layout instructions to process for a given request?The answer is "handlers"Let's look at our custom controller created earlier. For example - app code TestNamespace TestModulename Controller Test TestAs you remember, to add a new controller, we need to add a new route here in routes.xml and add the actual action class.In this case, we're going to return a page object and we can actually get the "handlers" information for that request from that page object.Let's get the layout and updates for this layout.The list of "handlers" is used for this page, and let's test it on the storefront. For this we must use special url key frontName, controller name Test, and the action name Test.As you can see, we have two handlers here: a default handler and a page-specific handler: unique test route id test testunique test route id test test consists of the route ID, controller name, and action name.This is a unique value for all requests.We can add some handlers to the page via XML or via code.Let's do it via code.As you can see, we now have three different handlers, so all layout settings with these handlers will be applied to this page.Now we can add a page-specific action handler to our module.As usual, we can take an example from the Magento framework.In module-catalog we can get something likeview frontend layout catalog product vgalleryThis is a good example.And we need to create the following folder view frontend layout.But the name of this file must match the name of this handler.Let's remove some unnecessary data here.Let's delete our test data.Let's clear the cache and check the storefront.Looks good.First of all, we can change the page layout.We have 1column layout type, but all the default layouts can be found in vendor module-theme view frontend page_layoutsHere we have 1 column, 2 columns, 3 columns, etc.Let's switch this to the 2columns-left.Let's flush the cacheNow we have another type of this layout, at least here there is a left column.We can also add our own layouts, but usually it is more than enough to use these layouts.As you can see, in addition to these XML layouts, "empty.xml" will be executed. This is the basic XML layout.We have 2columns-left which expand 1column.1column extends "empty", and "empty" is the base XML layout file.Almost every page in Magento is associated with the default handler.If you apply some layout instructions to the default handler instead, almost all pages will have these changes.But if you need to apply instructions only to a specific page, it is better to use the action handler for that page.Let's change this to 1 column and add some custom block here.To do this, we need to add another template file to the view folder.Let's add some HTML code here.Let's add this block to the page specific layout.For this we need to add the following information. So we can specify the name, but it is not a required field.If we don't specify a name, it will be generated automatically.Let's call this a test block. We need to specify a template.In our case it is TesstNamespace TestModuleName and then we need to specify the path to the template after the view frontend templates folder.In this case, it will be this information. But note that we need to specify a specific container.Let's use the content of the ReferenceContainer.And inside this container we can add this block.Let's clear the cache.Let's check this on a storefront.Works fine!Let's add template hints to this page.Now you can see the structure of this page, and as you can see, we haven't specified the class of this block.Here Magento default class is used but we can specify our own block in which we can add some information and in .phtml file we can get that information.Here's how it works.But what if we need to use some information in the block? We have several options for this.First of all, we can use - view models.This is something like a best practice recommended by Magento. View models can be used by passing the view model class as an argument to add a template block to the page layout configuration file.Let's add this here in the block.We can add additional arguments.Let's call it view_model.In the following example, the TestViewModel fragment is the view model class of the TestNamespace module TestModuleName passed as an argument to the block.Let's create view model class.As usual, we can check examples from magento module-catalog supplier module.For example.This one.And here is a good example of a view model.The view module class must always implement the argument interface, and then all public methods from that class will be available in the template.Let's add some public method.Let's go to the template.And here we can access the public methods of the view model.Let's check the result.Works fine.Another way to do this is to use a block class. A block class implements the rendering of a particular block, and an object of that class is responsible for rendering the block's output.Let's change the class of our block.Let's take an example from magento module-catalog.As you can see, this class must extend Template.Let's add our test block class.Let's add some public method.Let's get this information from the template file.Let's check.As you can see, we have another class mentioned for this book and everything works fine.Finally, we can pass multiple arguments using layout instructions.Examples of arguments can be taken from the template using the getData() method.Another way to get these arguments is to use the argument name in camel case.Let's try this. Let's add one more argument.Let's call it some_string.And in fact, it's better to specify the type of this argument.And now we have a some test string.Now in the template file we can do the following.We can use the getData method.some string or we can use the following format - getSomeString.Let's check this.And here's how it works!
You have completed 0% of the lesson
0%