Home Backend Development PHP Tutorial Magento Development Notes 3_PHP Tutorial

Magento Development Notes 3_PHP Tutorial

Jul 14, 2016 am 10:07 AM
magento view one time focus on and exist develop us notes this part

In this section, we focus on Layouts and Blocks in View.

Unlike other mainstream PHPMVC architectures, magento's ActionController does not pass data objects to the view, nor does it set properties in the View object. View obtains the information it needs through system modules.
The result of this design is that View is divided into Blocks and Templates. Blocks are PHP objects, and Templates are a mixture of PHP code and HTML (it can also be considered that PHP is used as a template language). Each Block is bound to a Template file. In a Phtml file, the PHP keyword $this will contain a reference to the Block corresponding to the Template.
Here is a quick example. View the template file app/design/frontend/base/default/template/catalog/product/list.phtml
You will see the following code
getLoadedProductCollection() ?>
count()): ?>
__("There are no products matching the selection.")?>
The getLoadedProudctController can be found in the corresponding block file
app/code/core/Mage/Catalog/Block/Product/List.php
public functiongetLoadedProductCollection()
{
return$this->_getProductCollection();
}
The _getProductCollection will instantiate models and get the data to the corresponding template.
Embedded Block
The real power of Blocks/Templates is the getChildHtml method. This allows us to include the secondary Block/Template in the main Block/Template (in xml format)
Blocks calling Blocks will form our entire HTMLlayout. Look at an example
App/design/frotend/base/default/template/page/1column.phtml
< htmlxmlns="http://www.w3.org/1999/xhtml" xml:lang="?php echo$this->getLang() ?>" lang=" getLang()?>"> getChildHtml('head') ?> getChildHtml('content') ?> getChildHtml('before_body_end') ?>
getAbsoluteFooter() ?>
The file is not long, but each call is $this->getChildHtml(…), which will include and render other blocks. These blocks may also call other blocks.
Layout                                                          
Although Block and Template are good, you may have the following questions
1. How do I tell Magento which Block is used in the page?
2. How do we tell Magento it is initial
3. How do I tell each Block to call the following block
At this time, the Layout object is needed. The Layout object is in Xml format and defines which Blocks are included in a page and which Blocks are responsible for rendering the page.
We did the previous Hello World project directly on Action Method. This time we create a simple HTMLtemplate to serve the module.
First create a file
App/design/frontend/base/default/layout/local.xml
Then write the following content
                                     
Then create another file
app/design/frontend/base/default/template/simple_page.phtml (note that it is consistent with the template in the configuration)
Write the following content
Untitled
                 
body { background-color:#f00; }
Finally, Aciton Controller is responsible for starting the layout process. Add the following two lines of code
public functionindexAction() {
//remove our previous echo
//echo'Hello Index!';
$this->loadLayout();
$this->renderLayout();
}
Clear the cache and reload the Hello World controller page. You can see that the background of the page is red, and the Html source code also corresponds to simple_page.phtml.
What happened?
Everything just seemed mysterious. Let's take a closer look at this process. First, we want to install the Layoutviewer module. This module is very similar to Configviewer.
Once installed, you can use the URL below
http://localhost/magento/helloworld/index/index?showLayout=page
This is a layout xml corresponding to the requested page. It consists of and tags. When you call the loadLaout method,
1. Generate a Layout xml
2. Instantiate the Block classes below and . Search for the tag name attribute, find the corresponding one in the global configuration file, and store it in the internal_blocks array of the Layout object.
3. If the tag contains an output attribute, its value is also added to the internal_blocks array of the Layout object.
In this way, when we call renderLayout in Action Controller, Mageno will iterate all the Blocks in the _blocks array and use its corresponding output attribute as the callback function. This is equivalent to conversion to Html, which means that the Template of the Block is the starting point of the output.
The following part involves how Block is instantiated, how Layout files are generated, and the end of output.
Block instantiation
In LayoutXml, there is a type of or which is equivalent to URI
URI will specify an address in the global configuration file. The first part of the URI is used to find the global configuration file and find the Page class name. The second part follows the first part and becomes a new class, which is then instantiated.
Take page/html as an example. First, Magento will look for the following
in the global configuration file
/global/blocks/page
Then find
                                                                                      
Mage_Page_Block
                                                                                   
This way we get the MagePageBlock class. Then, the second part of the URI is appended to it to become MagePageBlock_Html. This class will then be instantiated.
Blocks are also group classes in Magento, all sharing similar instantiation methods. This part will be introduced in detail later.
The difference between and
We talked about that both and can instantiate Block, so what is the difference between them.
First
This means that when requesting customeraccountindex, it should contain and under .
Apply what you learn
Having seen enough theory, let’s review our previous work.
                                       
This means we rewrote the root tag. The part guarantees that it will happen on every request. This may not be the effect we want.
If we visit any other page, we will also see a blank page, or a red background (like the previous helloworld page). So let's improve local.xml and make sure it is only used for the helloworld page. Modified as follows
                                       
Clear the cache and your other pages should be restored at this time.
Then apply to googbye Aciton Method
public function goodbyeAction() { $this->loadLayout(); $this->renderLayout(); }
Load http://localhost/magento/helloworld/index/goodbye
at this time
You will find that it is still a blank page. At this time we need to add an actionname to local.xml, the content is as follows
Clear the cache. At this time, the following two pages will have the same effect.
http://localhost/magento/helloworld/index/index
http://localhost/magento/helloworld/index/goodbye
Start output and getChildHtml
In the standard configuration, the output starts with the Block named root (this is the characteristic of the output). We have rewritten the root template template="simple_page.phtml"
The template will be obtained from the main directory of the current or base theme, such as
app/design/frontend/base/default/template
Usually you can add templates to your own theme or the default theme
app/design/frontend/default/default/template
app/design/frontend/default/custom/template
The base directory is the last directory to be searched. If magento cannot find it under other themes, it will return to the base directory. However, as mentioned before, you don't want to add to such directories because magento updates will overwrite them.
Add content block
Red Tragedy is boring. So let's add some content to the page. Change in local.xml as follows
 
I added two embedded Blocks in the root. Magento will distribute it and display a page for customer registration. To embed this Block in the root, we need to explicitly call it in simple_page.html. So we use the getChildHtml method of Block, as follows
                                                                                                                                                                         
Clear Cache and reload the page. At this time we see the registration page on a red background. There is also a Block below called top.links. Add the following
                                                                                                             
                                                                                                                                                                                                            
When we reload the page, we see that Links are rendered, but top.links does not have any rendering. This is because we did not add it in local.xml. In Layout, getChildHtml can only contain displayed Blocks as child blocks. This allows Magento to instantiate the blocks it wants, and also allows us to set different templates for the Block based on the display content
We can add Block to top.links in local.xml
                   
 
Clear the cache at this time and you can see the effect of the top.links module

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477872.htmlTechArticleLet’s focus on Layouts and Blocks in View in this section. Unlike other mainstream PHPMVC architectures, magento's ActionController does not pass data objects to views, nor does it set View objects...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to delete Xiaohongshu notes How to delete Xiaohongshu notes Mar 21, 2024 pm 08:12 PM

How to delete Xiaohongshu notes? Notes can be edited in the Xiaohongshu APP. Most users don’t know how to delete Xiaohongshu notes. Next, the editor brings users pictures and texts on how to delete Xiaohongshu notes. Tutorial, interested users come and take a look! Xiaohongshu usage tutorial How to delete Xiaohongshu notes 1. First open the Xiaohongshu APP and enter the main page, select [Me] in the lower right corner to enter the special area; 2. Then in the My area, click on the note page shown in the picture below , select the note you want to delete; 3. Enter the note page, click [three dots] in the upper right corner; 4. Finally, the function bar will expand at the bottom, click [Delete] to complete.

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? What should I do if the notes I posted on Xiaohongshu are missing? What's the reason why the notes it just sent can't be found? Mar 21, 2024 pm 09:30 PM

As a Xiaohongshu user, we have all encountered the situation where published notes suddenly disappeared, which is undoubtedly confusing and worrying. In this case, what should we do? This article will focus on the topic of &quot;What to do if the notes published by Xiaohongshu are missing&quot; and give you a detailed answer. 1. What should I do if the notes published by Xiaohongshu are missing? First, don't panic. If you find that your notes are missing, staying calm is key and don't panic. This may be caused by platform system failure or operational errors. Checking release records is easy. Just open the Xiaohongshu App and click &quot;Me&quot; → &quot;Publish&quot; → &quot;All Publications&quot; to view your own publishing records. Here you can easily find previously published notes. 3.Repost. If found

How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu How to add product links in notes in Xiaohongshu Tutorial on adding product links in notes in Xiaohongshu Mar 12, 2024 am 10:40 AM

How to add product links in notes in Xiaohongshu? In the Xiaohongshu app, users can not only browse various contents but also shop, so there is a lot of content about shopping recommendations and good product sharing in this app. If If you are an expert on this app, you can also share some shopping experiences, find merchants for cooperation, add links in notes, etc. Many people are willing to use this app for shopping, because it is not only convenient, but also has many Experts will make some recommendations. You can browse interesting content and see if there are any clothing products that suit you. Let’s take a look at how to add product links to notes! How to add product links to Xiaohongshu Notes Open the app on the desktop of your mobile phone. Click on the app homepage

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Why don't others see my attention on Weibo? -How to check visitor records on Weibo? Why don't others see my attention on Weibo? -How to check visitor records on Weibo? Mar 18, 2024 am 11:22 AM

Why don’t others see my attention on Weibo? We only need to classify the blogger into the group &quot;Follow quietly&quot; so that others will not see that we are following him. 1. Open Weibo on your mobile phone and click [Follow] on the homepage. 2. Click [Follow quietly] in &quot;My Group&quot;. How to check visitor records on Weibo? 1. The visitor recording function tested on Weibo is currently only visible to SVIP and VVIP, and is only open to some users. 2. Users can find the visitor record entrance in [More Functions] under the personal center, where they can view the number of visitors, the visitors, and the people who visit more frequently. 3. This function is only open to SVIP and VVIP users, and is temporarily unavailable to ordinary users and ordinary member users. 4. In short, Weibo test visitors

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

Summary of the five most popular Go language libraries: essential tools for development Summary of the five most popular Go language libraries: essential tools for development Feb 22, 2024 pm 02:33 PM

Summary of the five most popular Go language libraries: essential tools for development, requiring specific code examples. Since its birth, the Go language has received widespread attention and application. As an emerging efficient and concise programming language, Go's rapid development is inseparable from the support of rich open source libraries. This article will introduce the five most popular Go language libraries. These libraries play a vital role in Go development and provide developers with powerful functions and a convenient development experience. At the same time, in order to better understand the uses and functions of these libraries, we will explain them with specific code examples.

See all articles