如何在 magento 2 的 phtml 文件中调用块函数?
How to call block functions in phtml file in magento 2?
我正在 magento 2 中创建自定义模块。我想在 phtml 文件中调用块函数。但它不起作用。请帮助我。
这是我在 adminhtml 文件夹文件中的块。
namespace Question\Topic\Block\Adminhtml;
class Topic extends \Magento\Framework\View\Element\Template {
public function getSample() {
return "abhishek";
}
}
我在 view/adminhtml/layout 中的 topic_order_view.xml 文件是
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="view" template="Questions_Topic::view.phtml" />
</referenceContainer>
</body>
</page>
这是我在 Controller/Adminhtml/Order/view 中的控制器。php---
namespace Question\Topic\Controller\Adminhtml\Order;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
class View extends \Magento\Backend\App\Action
{
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @var scopeConfig
* Needed to retrieve config values
*/
protected $scopeConfig;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
ScopeConfigInterface $scopeConfig // Needed to retrieve config values
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->scopeConfig = $scopeConfig; // Needed to retrieve config values
}
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Orders')); //
return $resultPage;
}
}
我的 view.phtml 文件在 view/adminhtml/templates/order/view.phtml
<?php
//echo $this->getSample();
echo $block->getSample();
?>
<h1>Hello </h1>
显示的是你好这个词。但不回显上面的代码块
提前致谢..
您应该 'tell' 您要将哪个块传递给内容的布局。
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Question\Topic\Block\Adminhtml\Topic" name="question.topic.view" template="Questions_Topic::view.phtml" />
</referenceContainer>
</body>
</page>
我正在 magento 2 中创建自定义模块。我想在 phtml 文件中调用块函数。但它不起作用。请帮助我。
这是我在 adminhtml 文件夹文件中的块。
namespace Question\Topic\Block\Adminhtml;
class Topic extends \Magento\Framework\View\Element\Template {
public function getSample() {
return "abhishek";
}
}
我在 view/adminhtml/layout 中的 topic_order_view.xml 文件是
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="view" template="Questions_Topic::view.phtml" />
</referenceContainer>
</body>
</page>
这是我在 Controller/Adminhtml/Order/view 中的控制器。php---
namespace Question\Topic\Controller\Adminhtml\Order;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
class View extends \Magento\Backend\App\Action
{
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @var scopeConfig
* Needed to retrieve config values
*/
protected $scopeConfig;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
ScopeConfigInterface $scopeConfig // Needed to retrieve config values
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->scopeConfig = $scopeConfig; // Needed to retrieve config values
}
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Orders')); //
return $resultPage;
}
}
我的 view.phtml 文件在 view/adminhtml/templates/order/view.phtml
<?php
//echo $this->getSample();
echo $block->getSample();
?>
<h1>Hello </h1>
显示的是你好这个词。但不回显上面的代码块
提前致谢..
您应该 'tell' 您要将哪个块传递给内容的布局。
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Question\Topic\Block\Adminhtml\Topic" name="question.topic.view" template="Questions_Topic::view.phtml" />
</referenceContainer>
</body>
</page>