magento函數getChildHtml() 與getChildChildHtml() 的用法

來源:互聯網
上載者:User

標籤:使用者註冊   href   net   custom   frontend   tag   protect   blank   sim   

  1. getChildHtml(‘a‘) //載入as=‘a’的block  
  2. getChildHtml(‘‘); //載入所有的子block  
  3. getChildChildHtml (‘container1‘ ,  ‘b‘ ,  true ,  true); //載入子block container1以及他的子block(as=‘b‘);  
  4. getChildChildHtml (‘container1‘ ,  ‘‘ ,  true ,  true);  //載入子block container1,以及下面的所有子block  
    1. <?php  echo  $this -> getChildChildHtml ( ‘container1‘ ,  ‘‘ ,  true ,  true )  ?>  
    2. <?php  echo  $this -> getChildChildHtml ( ‘container2‘ ,  ‘‘ ,  true ,  true )  ?>
  5. 在Magento預設的配置下,HTML輸出是從名為“root”的塊開始(其實是因為這個塊 擁有output屬性【EFISH註:任何一個擁有output屬性的塊都是頂層塊,在擁有多個頂層塊的情況下Magento將按照塊定義的先後順序輸出 HTML】)。我們覆蓋了“root”塊的模板

    template="../../../../../code/local/Alanstormdotcom/Helloworld/simple_page.phtml"
    模板檔案的尋找路徑是當前主題(theme)的根目錄,Magento預設設定時這裡
    app/design/frontend/base/default

    為頁面加入內容

    到目前為止,我們的頁面都比較無聊,啥也沒有。我們來為頁面加點有意義的內容。修改 local.xml如下
    <helloworld_index_index>
    <reference name="root">
    <block type="page/html" name="root" template="../../../../../code/local/Alanstormdotcom/Helloworld/simple_page.phtml">
    <block type="customer/form_register" name="customer_form_register" template="customer/form/register.phtml"/>
    </block>
    </reference>
    </helloworld_index_index>

    我們在“root”塊裡面嵌套了一個塊“customer_form_register”。這個塊是Magento本來就有的,包含了一張使用者註冊表單。 我們把這個塊嵌套進來,那麼我們在模板檔案裡面就能用這個塊的內容。使用方法如下,修改simple_page.phtml
    <body>
    <?PHP echo $this->getChildHtml(‘customer_form_register‘); ?>
    </body>

    這裡“getChildHtml”的參數就是要引入的塊的名字,使用起來相當方便。清空Magento緩衝,重新整理hello world頁面,你應該在紅色背景上看到使用者註冊表單。Magento還有一個塊,叫做“top.links”,讓我們把它也加進來。修改 simple_page.html
    <body>
    <h1>Links</h1>
    <?php echo $this->getChildHtml(‘top.links‘); ?>
    <?php echo $this->getChildHtml(‘customer_form_register‘); ?>
    </body>

    重新整理頁面,你會發現<h1>Links</h1>顯示出來了,但是“top.links”什麼都沒有顯示。那是因為我們並沒有把 這個塊引入到local.xml,所以Magento找不到這個塊。“getChildHtml”的參數一定要是當前頁面的布局檔案中聲明過的塊。這樣的 話Magento就可以只執行個體化需要用到的塊,節省了資源,我們也可以根據需要為塊設定不同的模板檔案。

    我們修改local.xml檔案如下
    <helloworld_index_index>
    <reference name="root">
    <block type="page/html" name="root" template="../../../../../code/local/Alanstormdotcom/Helloworld/simple_page.phtml">
    <block type="page/template_links" name="top.links"/>
    <block type="customer/form_register" name="customer_form_register" template="customer/form/register.phtml"/>
    </block>
    </reference>
    </helloworld_index_index>

    清空Magento緩衝,重新整理頁面,你會看到一排連結顯示出來了。【EFISH註:如果你細心一點的話你會發現“top.links”塊沒有template屬 性,那是因為這個塊的類中一定定義了預設的模板
    protected function _construct()
    {
    $this->setTemplate(‘page/template/links.phtml‘);
    }

magento函數getChildHtml() 與getChildChildHtml() 的用法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.