magento函数getChildHtml() 与getChildChildHtml() 的用法

  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');
    }

posted @ 2017-03-15 17:44  思风雨  阅读(922)  评论(0编辑  收藏  举报