PART 1.2.3 – Templates

Overriding and customisations

There is two places where you can find templates. Some of template use phtml files, some of them use knockout.js.

Security

To prevent XSS issues, Magento recommends the following rules of escapingHTML content in templates:

Escape data using the following methods:

  • $block->escapeHtml()
  • $block->escapeQuote()
  • $block->escapeUrl()
  • $block->escapeXssInUrl()

If a method indicates that the contents are escaped, do not escape: getTitleHtml(), getHtmlTitle() (the title is ready for the HTML output)

 

Example

<?php echo $block->getTitleHtml() ?>

 

<?php echo $block->getHtmlTitle() ?>

 

<?php echo $block->escapeHtml($block->getTitle()) ?>

 

<h1><?php echo (int)$block->getId() ?></h1>

 

<?php echo count($var); ?>

 

<?php echo ‘some text’ ?>

 

<?php echo “some text” ?>

 

<a href=”<?php echo $block->escapeXssInUrl($block->getUrl()) ?>”>

 

<?php echo $block->getAnchorTextHtml() ?>

 

</a>

Magento 2.2

The upcoming release of Magento 2.2 will deprecate these functions.Please check back on this page (http://devdocs.magento.com/guides/v2.1/frontend-dev-guide/templates/template-security.html) after the 2.2 release for updated documentation on new escape functions.

Variables

Magento 2’s variables used in template shouldn’t have underscore “_” in the name what is a different comparing to Magento 1.

Magento 1

Magento 2

$_item

$item

Exercise 6.0

Remove product name from items on category page.

Advertisements