Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento 2在模板文件phtml中获取块

在本主题中,我真的想通过php代码向您介绍如何phtmlMagento 2中的模板文件中获取块。我将在这里举两个例子。

例1

如果您需要调用模板块helloworld.phtmlMageplaza_HelloWorld,请使用以下代码:

echo $this->getLayout()
          ->createBlock('Mageplaza\HelloWorld\Block\HelloWorld')
          ->setTemplate('Mageplaza_HelloWorld::helloworld.phtml')
          ->toHtml();

例2

如果您需要在Magento 2中的CMS静态块或CMS页面中调用模板块,请使用以下代码:


block class="Mageplaza\HelloWorld\Block\HelloWorld" name="your_block_name" template="Mageplaza_HelloWorld::helloworld.phtml"

希望这是从模板phtml文件中获取块的有用文章。如果您对文章或任何问题有任何疑问,请使用下面的评论部分!

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论