Layout instruction 之 argument 和 action

本篇我们将通过一个 demo 来演示如何使用 layout 指令 ——— argument 和 action 。

本篇基于 创建 Magento 2 Hello World Simple Module 文章中的简单模块。

创建 Controller

File:app\code\ThankIT\HelloWorld\Controller\Layout\Instruction.php

<?php
namespace ThankIT\HelloWorld\Controller\Layout;

class Instruction extends \Magento\Framework\App\Action\Action
{

    protected $resultPageFactory;

    /**
     * Constructor
     *
     * @param \Magento\Framework\App\Action\Context  $context
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        $this->resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }

    /**
     * Execute view action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        return $this->resultPageFactory->create();
    }
}

创建 layout

File:app\code\ThankIT\HelloWorld\view\frontend\layout\helloworld_layout_instruction.xml

<?xml version="1.0" ?>
<page layout="1column" 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="ThankIT\HelloWorld\Block\Layout\Instruction" name="layout.instruction" template="ThankIT_HelloWorld::layout/instruction.phtml">
                <arguments>
                    <argument name="label" xsi:type="string">Block Label</argument>
                </arguments>
                <action method="doSomething">
                    <argument translate="true" name="thing" xsi:type="string">Running</argument>
                </action>
            </block>
        </referenceContainer>
    </body>
</page>

创建 Block

File:app\code\ThankIT\HelloWorld\Block\Layout\Instruction.php

<?php
namespace ThankIT\HelloWorld\Block\Layout;

use Magento\Framework\View\Element\Template;

class Instruction extends Template
{
    public function doSomething($thing)
    {
        // do something ...
        $this->setData('some_thing', $thing);
    }
}

创建 view

File:app\code\ThankIT\HelloWorld\view\frontend\templates\layout\instruction.phtml

<?php echo $block->getLabel();?>
<?php //等价于//echo $block->getData('label');?>
<br/>
<?php echo $block->getSomeThing();?>

测试

刷新缓存后,访问 http://example/helloworld/layout/instruction/

会看到:

layout-instruction-argument-action

注意点

Use block object methods to set block properties

There are two ways to access block object methods:

  • using the <argument> instruction for <block> or <referenceBlock>
  • using the <action> instruction. This way is not recommended, but can be used for calling those methods, which are not refactored yet to be accessed through <argument>.

Do not use <action>, if the method implementation allows calling it using <argument> for <block> or <referenceBlock>.

参考文档

Common layout customization tasks
Layout instructions

发表评论

电子邮件地址不会被公开。 必填项已用*标注