Magento Call custom block inside another custom block

I am new to magento, so please forgive me if I have asked anything wrong. I have created a custom module for instagram Login, I just want to call a particular block from that instagram module to another layout file. I have called that block printed in my view page using

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

My block got called but functionalities are working and form is not submitting.

My custom module layout file. Instagram.xml

<?xml version="1.0"?>
<layout version="0.1.0">
<default>
      <reference name="content">
      </reference>
</default>
     <instagram_index_instagram_signup>  
        <reference name="head">
            <action method="addJs"><script>magenthemes/jquery/plugins/jquery.cookie.js</script></action>
        </reference>
        <reference name="content">
             <block type="core/template" name="instagram.signup" template="instagramlogin/Instagram_signup.phtml"></block>
        </reference>
    </instagram_index_instagram_signup>  
</layout>

I want to call the above block to another module layout file: My another module layout page:

             <marketplace_vendor_login>
    <reference name="head">
            <action method="addJs"><script>magenthemes/jquery/plugins/jquery.cookie.js</script></action>
    </reference>
     <remove name="right"/>
    <remove name="left"/>
     <reference name="before_body_end">
        <block type="facebookfree/init" name="belvg_facebookfree_block">
            <action method="setTemplate" ifconfig="facebookfree/settings/enabled" ifvalue="1">
                <template>belvg/facebookfree/block.phtml</template>
            </action>
        </block>
    </reference>
    <reference name="root">
        <action method="setTemplate"><template>page/1column.phtml</template></action>
    </reference>
    <reference name="content">
    <block type="marketplace/vendor_login" name="customer_form_login" template="nbmpmarketplace/login.phtml" >
        <block type="facebookfree/links_button" name="belvg_facebookfree_button" template="belvg/facebookfree/form/button.phtml" />
          <block type="core/template" name="instagram.signup" as="media_new" template="instagramlogin/Instagram_signup.phtml"></block>
    </block>
    </reference>
</marketplace_vendor_login>

Here I have called my custom block, then printed in my view page as:

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

My controller code for instagram:

class Blazedream_Instagram_IndexController extends Mage_Core_Controller_Front_Action
        {
public function instagram_signupAction()
{
    if ($this->getRequest()->isPost()){  
        if($this->getRequest()->getPost('instagram-email')){
            $instagram_email  = $this->getRequest()->getPost('instagram-email');
            $customer_model = Mage::getModel('instagram/instagram');
            $site_customer = $customer_model->checkCustomer($instagram_email);
            if($site_customer){
                $instagram_customer = Mage::getModel('instagram/instagram')->getCollection()
                                                  ->addFieldToFilter('customer_id', array('eq' => $site_customer));
                if(count($instagram_customer)==1){
                    Mage::getSingleton('core/session')->setInstagramEmail($instagram_email);
                    $this->_redirect('instagram/index');    
                }else{
                    Mage::getSingleton('customer/session')->addError('Email Id Already Registered.');
                    $this->_redirect('sellerlogin');    
                }
            }else{
                Mage::getSingleton('core/session')->setInstagramEmail($instagram_email);
                $this->_redirect('instagram/index');
            }
        }
    }
    $this->loadLayout();
    $this->getLayout()->getBlock('instagram.signup');
    $this->renderLayout();
}
 }

It is not coming into this controller function, That i have used for instagram module.

My view page: instagramlogin.phtml

      <div style="display: none">
    <style type="text/css">
        #cboxContent.newsletterbox {
        <?php if($backgroundImage){?> background-image: url(<?php echo Mage::getBaseUrl('media').'/wysiwyg/magenthemes/newsletter/'.$backgroundImage;?>);
        <?php }?> background-position: left top;
            background-repeat: no-repeat;
            background-color: <?php echo $backgroundColor;?>;
        }
    </style>
    <div id="mt_instagram" class="block block-subscribe">
        <div class="row">
            <div class="left-newletters col-lg-8 col-md-8 col-sm-8 col-xs-8">
                <div class="block-title">
                    <strong><span><?php echo $this->__('Join our Mail List Through Instagram!') ?></span></strong>
                </div>
                <div class="row-none">
                    <div class="popup_message">
                        <div class="intro">
                            <?php echo $desc; ?>
                        </div>
                        <form action="<?php echo Mage::getBaseUrl().'instagram/index/instagram_signup'; ?>" method="post" id="instagram-validate-detail">
                            <div class="block-content">
                                <div class="input-box">
                                    <input name="instagram-email" type="text" id="mt-newsletter" placeholder="Enter your email id"
                                           value="" class="input-text required-entry validate-email"/>


                                    <div class="actions">
                                        <button type="submit" title="<?php echo $this->__('Subscribe') ?>"
                                                class="button">
                                            <span><span><?php echo $this->__('Subscribe') ?></span></span></button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

I am getting my custom form also..But the controller is not getting called and functionalists are not working, only I am getting the form.

Can anyone help Me?


This is my first ever answer on stackoverflow, I will try my best to explain.

To start with, let me brief you about the block calls in Magento; any particular type of Block(.php files in Block folder) has a corresponding content/template (.phtml in template folder) file to render the data fetched from Model (.php in Model folder).

In your layout (.xml for your module in layout folder), you have added a block that is of type (core/template) so it will call the file Mage_Core_Block_Template.php and can access all its methods. However, if you want to create a custom block you can update layout (instagram module) file like this.

    <layout version="0.1.0">
    <default>
          <reference name="content">
          </reference>
    </default>
         <instagram_index_instagram_signup>  
            <reference name="head">
                <action method="addJs"><script>magenthemes/jquery/plugins/jquery.cookie.js</script></action>
            </reference>
            <reference name="content">
                 <block type="instagram/instagram" name="instagram.signup" template="instagramlogin/Instagram_signup.phtml"></block>
            </reference>
        </instagram_index_instagram_signup>  
    </layout>

And create a file in Block folder with name Blazedream_Instagram_Block_Instagram.php and then you will be able to call custom methods to the block template. Ex.-

    class Blazedream_Instagram_Block_Instagram extends Mage_Core_Block_Template
    {
      public function customMethod()
      {
        $response = array('a', 'b', 'c');

        $this->getResponse()->setHeader('Content-type','application/json', true);
        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
        return;
      }
    }

Update you controller action call to

    $this->loadLayout();
    $this->renderLayout();

Now, to add this block inside another custom module, update its layout like

    <fname_controller_action>
         //path controller path on which you want to add/update the block
        <update handle="my_custom_handle" />
    </fname_controller_action>

    <my_custom_handle>
        <reference name="content">
            <reference name="where_you_want_to_add">
                <block type="instagram/instagram" name="my.custom.block" as="instagram.signup" template="template_path/instagram.phtml"/>
            </reference>
        </reference>
    </my_custom_handle>

or add a constructor inside the block file Blazedream_Instagram_Block_Instagram.php like -

    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('template/instagram.phtml');
    }

and call the block in any template file with the below code

    echo $this->getLayout()->createBlock('instagram/instagram')->toHtml();

You might need to update your layout file in this case.

If you do everything correctly, you should be able to call $this->customMethod() in the template file instagram.phtml

Thanks! Pardon me if I could not explained it properly.

链接地址: http://www.djcxy.com/p/59492.html

上一篇: 为什么this()和super()必须是构造函数中的第一条语句?

下一篇: Magento在另一个自定义块内调用自定义块