簡體   English   中英

如何在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 / adminhtml / templates / order / view.phtml中的view.phtml文件

<?php
//echo $this->getSample();
echo $block->getSample();

?>

<h1>Hello </h1>

它顯示Hello字。 但不回顯上面的代碼

提前致謝..

您應該“告訴”您要傳遞給內容的塊的布局。

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM