I was working on creating a simple Hello world module in Magento 2. Tutorials from other websites, there was only instructions to create the module and then I found the page title was empty.
I did a little search in the core files and found the code to set page title.
SET PAGE TITLE VIA BLOCK
$this->pageConfig->getTitle()->set(__('Hello World'));
You need to place this code inside your _prepareLayout() function in your Block file. Below is the complete function.
public function _prepareLayout()
{
//set page title
$this->pageConfig->getTitle()->set(__('Hello Index Test'));
return parent::_prepareLayout();
}
SET PAGE TITLE VIA LAYOUT XML
Open your xml file inside your layout folder and place this code above the body tag.
<head>
<title>SAMPLE TITLE</title>
</head>
NOTE: When you try to add title via both the methods first preference will be given to the block method(php file).
I did a little search in the core files and found the code to set page title.
SET PAGE TITLE VIA BLOCK
$this->pageConfig->getTitle()->set(__('Hello World'));
You need to place this code inside your _prepareLayout() function in your Block file. Below is the complete function.
public function _prepareLayout()
{
//set page title
$this->pageConfig->getTitle()->set(__('Hello Index Test'));
return parent::_prepareLayout();
}
SET PAGE TITLE VIA LAYOUT XML
Open your xml file inside your layout folder and place this code above the body tag.
<head>
<title>SAMPLE TITLE</title>
</head>
NOTE: When you try to add title via both the methods first preference will be given to the block method(php file).
No comments:
Post a Comment