在另一个模板中调用一个块

时间:2013-03-11 11:29:56

标签: magento module block magento-1.7

我只是创建一个模块名称引用。现在我想将引用块放到另一个模块模板文件名success.phtml。可以吗?

referral.xml(在推荐模块中)

 <?xml version="1.0"?>
    <layout version="0.1.0">

            <checkout_onepage_success>
<reference name="checkout.success">
                    <block type="referral/referral" name="referralCallLink"><action method="referralCallLink"></action></block>
                </reference>
            </checkout_onepage_success>
            <!--block type="referral/referral" name="referralAddSession"><action method="referralAddSession"></action></block-->

    </layout>

success.phtml

<?php if($hasBoughtMCash): ?>
<div> Your 
<?php echo implode(', ',$hasBoughtMCash); ?>
 purchase is successful.
</div>
<?php endif; ?>
<h2>Share in Facebook and Earn for Free MCash!</h2>
<?php echo $this->getChildHtml(); ?>

Referral.php(块)

public function referralCallLink() //success page
    {
    ...

    $collection7 = Mage::getModel('referral/referrallink')->getCollection();
    $collection7->addFieldToFilter('customer_id', array('eq' => $cust_id));
    $collection7->addFieldToFilter('grouped', array('eq' => $grouped));

        foreach($collection7 as $data3)
        {
         $product = $data3->getData('product');
         $link = $data3->getData('link');
         $imageurl = $data3->getData('url');            
        //facebook
         $title=urlencode('Shop, Save and Get Rewarded at MRuncit.com');
         $url=urlencode($link);
         $summary=urlencode('I just bought  '.$product.' from MRuncit.com and earned some MReward Points!');
         $image=urlencode($imageurl);

        ?>
        <p>
        <a href="http://www.facebook.com/sharer.php?s=100&amp;p[title]=<?php echo $title;?>&amp;p[summary]=<?php echo $summary;?>&amp;p[url]=<?php echo $url; ?>&amp;p[images][0]=<?php echo $image;?>','sharer','toolbar=0,status=0,width=548,height=325');" target="_blank">
        <img src="<?php echo $imageurl;?>" width="30">
        I just bought  <?php echo $product; ?> from MRuncit.com and earned some MReward Points!
        </a>
        </p>
        <?php

        }
    }

结果 enter image description here

1 个答案:

答案 0 :(得分:0)

您应该在布局XML中将块创建为成功块的子代:

<layout_handle_of_the_success_page>
    <reference name="name_of_the_success_block_in_layout">
        <block type="your/referral_block" />
    </reference>
</layout_handle_of_the_success_page>

然后你可以在success.phtml中插入以下行:

<?php echo $this->getChildHtml('referral'); ?>

示例XML中有一些名称必须替换为您自己的名称:

  • layout_handle_of_the_success_page - 您将在相应模块的布局XML中找到它。它的格式应为module_controller_action - &gt; checkout_onepage_success
  • name_of_the_success_block_in_layout - 同样来自布局XML,查找带有success.phtml模板的块及其name属性 - &gt; checkout.success
  • your/referral_block - 这是您希望以module/class - &gt;形式插入的块的类别名介/介