magento block controller layout

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/terry_water/article/details/81774108


1.
./code/community/Zendesk/Zendesk/Block/Adminhtml/Dashboard/Grids.php

$this->getLayout()->createBlock('zendesk/adminhtml_dashboard_tab_tickets_grid_all')->toHtml();


./code/community/Zendesk/Zendesk/Block/Adminhtml/Create/Order.php:40:        

return $this->getLayout()->createBlock('adminhtml/widget_button')->setData($addButtonData)->toHtml();


./code/community/Zendesk/Zendesk/Block/Adminhtml/Create/Order.php:40:        

return $this->getLayout()->createBlock('adminhtml/widget_button')->setData($addButtonData)->toHtml();

class Aschroder_SMTPPro_Block_Adminhtml_Test
    extends Mage_Adminhtml_Block_System_Config_Form_Field
{


    protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
    {
        $this->setElement($element);
        $buttonHtml = $this->_getAddRowButtonHtml($this->__('Run Self Test'));
        return $buttonHtml;
    }


   protected function _getAddRowButtonHtml($title)
  {

        $buttonBlock = $this->getElement()->getForm()->getParent()->getLayout()->createBlock('adminhtml/widget_button');

        $_websiteCode = $buttonBlock->getRequest()->getParam('website', null);

        $params = array();

        if(!empty($_websiteCode)) {
            $params['website'] = $_websiteCode;
        }

        // TODO: for real multi-store self-testing, the test button (and other configuration options)
        // should probably be set to show in website. Currently they are not.
        $url = Mage::helper('adminhtml')->getUrl("*/smtp_test/index", $params);

        $buttonHtml = $this->getLayout()->createBlock('adminhtml/widget_button')
                    ->setType('button')
                    ->setLabel($this->__($title))
                    ->setOnClick("window.location.href='".$url."'")
                    ->toHtml();

        return $buttonHtml;
    }

}


直接创建一个block,然后加入到配置中

```
public function indexAction()
{
//Get current layout state
$this->loadLayout();
 
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'my_block_name_here',
array('template' => 'activecodeline/developer.phtml')
);

// 从layout中得到一个已有的block,然后将上面创建的加入的写法。
$this->getLayout()->getBlock('content')->append($block);
 
//Release layout stream... lol... sounds fancy
$this->renderLayout();
}

```


```
    public function indexAction(){
        $this->loadLayout();
        $this->_initLayoutMessages('customer/session');
        $this->renderLayout();
    }
```

2.代码剖析

上面是在controller和block可以被执行中执行的

2.1 $this->getLayout()
2.1.1 block: Mage_Core_Block_Abstract

    /**
     * Retrieve layout object
     *
     * @return Mage_Core_Model_Layout
     */
    public function getLayout()
    {
        return $this->_layout;
    }


返回的是:Mage_Core_Model_Layout


2.1.2 controller: Mage_Core_Controller_Varien_Action

/**
     * Retrieve current layout object
     *
     * @return Mage_Core_Model_Layout
     */
    public function getLayout()
    {
        return Mage::getSingleton('core/layout');
    }

本质就是:单例模式的core/layout  model:
Mage::getSingleton('core/layout');

2.2 loadLayout

猜你喜欢

转载自blog.csdn.net/terry_water/article/details/81774108