What are the conventions for variable naming?
When naming classes and methods, it is taken to use the standard called “CamelCase”. According to the standard, each word in the middle of the phrase should begin with a capital letter. Besides, any separator, for example, underscore is not allowed to use.
If you’d like to get your code more readable, you should follow the next rules:
- Local or “private” variables and methods are prefixed with underscore: $_loacalVar , $_cartHelper, _prepareLayout;
- Class name always begins with a capital letter: Breadcrumbs, AbstractItem, Card;
- Methods that return logic values (true or false) are prefixed with “is”: isContentMode, isProductMode;
- Methods that check content are prefixed with “has”: hasProductUrl, hasError;
- Methods that return any value are prefixed with “get<Return_type>”: getProduct, getChildHtml;
- Methods that perform conversion to a type are prefixed with “to”: toHtml, toOptionArray.
The rules mentioned above are used both in Magento 1.x and 2.x. There are no differences at all.
Magento 2 Migration
Take your online store to the next level with BelVG Magento 2 Migration
Visit the pageWhat does $this refer to in template files?
In fact, $this refers to the parent PHP block object where the template has been declared. There are some changes that have been applied in Magento 2.x. If you’d like modules and themes to pass validation magento 2 coding-standard, it is recommended to use $block instead of $this, which in turn refers to $this->_currentBlock.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// file: app/code/Magento/Search/view/frontend/templates/term.phtml <?php if (sizeof($block->getTerms()) > 0): ?> // getTerms method is implemented in app/code/Magento/Search/Block/Term.php class <ul class="search-terms"> <?php foreach ($block->getTerms() as $_term): ?> <li class="item"> <a href="<?php /* @escapeNotVerified */ echo $block->getSearchUrl($_term) ?>" style="font-size:<?php /* @escapeNotVerified */ echo $_term->getRatio()*70+75 ?>%;"> <?php echo $block->escapeHtml($_term->getName()) ?> </a> </li> <?php endforeach; ?> </ul> <?php else: ?> //... |
1 2 3 4 5 6 7 8 |
//file: app/code/Magento/Search/Block/Term.php // .. public function getTerms() // implementation of the method, which is called in the template, mentioned above { $this->_loadTerms(); return $this->_terms; } // ... |
How can another template be included inside the current one?
The only thing you should do is to declare a descendant block in a parent block by determining attributes: type, name, as, template, after, before.
Note: the attributes “after” and “before” (for block positioning) will work only while calling method “getChildHtml” without parameters!
1 2 3 4 |
// magento 1.x <?php echo $this->getChildHtml(); ?> // magento 2.x <?php echo $block->getChildHtml(); ?> |
Then, it’s necessary to determine a descendant block for a parent template:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!-- magento 1.x --> <!-- app/design/frontend/<package>/<theme_name>/layout/local.xml --> <catalog_product_view> <!—define a necessary page --> <reference name="footer"> <!—parent block ---> <block type="newsletter/subscribe" name="newsletter" as="newsletter" template="newsletter/footer-subscribe.phtml"/> <!—inside define a descendant block and determine necessary attributes --> </reference> </catalog_product_view> <!-- magento 2.x --> <!-- app/design/frontend/<vendor>/<theme_name>/Magento_Theme/layout/default.xml --> <body> <referenceContainer name="footer"> <block class="Magento\Newsletter\Block\Subscribe" name="form.subscribe" as="subscribe" before="-" template="footer-subscribe.phtml"/> </referenceContainer> </body> |
Moreover, it is possible to call descendant template output in any necessary place of a parent template. You should call getChildHtml method and pass the name of the descendant block as the first parameter.
1 2 3 4 5 6 7 |
// magento 1.x // parent template :app/design/frontend/<package>/<theme_name>/template/page/html/footer.phtml <?php echo $this->getChildHtml('newsletter'); ?> // magento 2.x // parent template:app/design/frontend/<vendor>/<theme_name>/Magento_Theme/templates/html/footer.phtml <?php echo $block->getChildHtml('form.subscribe'); ?> |
Magento Custom Development
Take your online store to the next level with BelVG Magento Custom Development
Visit the page
Great information! Thank you for sharing. (y)