How to get store name in template?
you need to use the instance of \Magento\Framework\App\Config\ScopeConfigInterface
in your block:
Create the method getStoreName()
public function getStoreName()
{
return $this->_scopeConfig->getValue(
'general/store_information/name',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
and call in your template echo $this->getStoreName()
Use store manager, which holds information about active store. If custom block is not inherited from Template
block, inject dependency on \Magento\Store\Model\StoreManagerInterface
in construct.
<?php
namespace VendorName\ModuleName\Block;
class CustomBlock extends \Magento\Framework\View\Element\Template
{
/**
* Get current store name.
*
* @return string
*/
public function getCurrentStoreName()
{
return $this->_storeManager->getStore()->getName();
}
}
Then in template:
<?php
/**
* @var $block \VendorName\ModuleName\Block\CustomBlock
*/
echo "<h1>Current store name is '{$block->getCurrentStoreName()}'</h1>";
?>
To get a store configuration value like general/store_information/name
you can use the following
$config = new \Magento\Framework\App\Config\ScopeConfigInterface();
echo $config->getValue('general/store_information/name');
However, doing this from a block or helper would be cleaner. Below is a helper class that would exist in your own custom module
namespace [Namespace]\[Module]\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* Retrieve store name
*
* @return string|null
*/
public function getStoreName()
{
return $this->scopeConfig->getValue(
'general/store_information/name',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}
}
Which you would inject as dependency in your block class