0

My home.phtml file

echo $this->getLayout()->createBlock('banner/Monblock')
->setTemplate('banner/afficher.phtml')->toHtml();

my layout file.

<?xml version="1.0" ?>
<layout version="0.1.0">
  <test_adminhtml_index_index>
     <reference name="content">
             <block type="banner/Myblock"  name="afficher_monbloc"
                  template="banner/afficher.phtml"/>
                   <block type="banner/adminhtml_grid" name="banner" />
      </reference>

   </test_adminhtml_index_index>

   <reference name="content">
             <block type="banner/Monblock"  name="afficher_monbloc"
                  template="banner/afficher.phtml"/>
                   <block type="banner/adminhtml_grid" name="banner" />
      </reference>

       <default>
          <reference name="content">
          </reference>
      </default>
      <banner_index>
           <reference name="content">
               <block type="banner/Monblock"  name="afficher_monbloc"
                  template="banner/afficher.phtml"/>
                  <block type="test/test"  name="test"
                          template="test/tes2.phtml" />
           </reference>
      </banner_index>
</layout>

My block file

class Banner_Banner_Block_Monblock extends Mage_Core_Block_Template
{
     public function methodblock()
     {
         return 'informations about my block !!' ;
     }
}

config.xml

<?xml version="1.0"?>
  <config>
     <modules>
        <Banner_Banner>
          <version>1.0.0</version>
        </Banner_Banner>
     </modules>
     <frontend>
       <routers>
          <banner>
              <use>standard</use>
              <args>
                 <module>Banner_Banner</module>
                 <frontName>banner</frontName>
              </args>
           </banner>
       </routers>
       <layout>
            <updates>
                <Banner_Banner>
                    <file>Banner.xml</file>
                </Banner_Banner>
            </updates>
        </layout>   
    </frontend>
    <admin>
     <routers>
         <banner>
            <use>admin</use>
            <args>
               <module>Banner_Banner</module>
               <frontName>admintest</frontName>
            </args>
         </banner>
      </routers>
 </admin>
 <adminhtml>
   <layout>
      <updates>
          <banner>
              <file>banner.xml</file>
           </banner>
      </updates>
   </layout>
   <menu>
      <banner translate="title" module="adminhtml">
         <title>My Banner</title>
         <sort_order>100</sort_order>
         <children>
             <set_time>
                   <title>Add Banner</title>
                   <action>admintest/adminhtml_index</action>
              </set_time>
          </children>
       </banner>
    </menu>
</adminhtml>
     <global>
      <models>
<banner>
     <class>Banner_Banner_Model</class>
     <resourceModel>Banner_mysql4</resourceModel>
 </banner>
<banner_mysql4>
     <class>Banner_Banner_Model_Mysql4</class>
     <entities>
         <banner>
           <table>banner</table>
         </banner>
      </entities>
</banner_mysql4>
</models>
    <!-- allow the plugin to read and write -->
<resources>
        <!-- connection to write -->
        <test_write>
            <connection>
                <use>core_write</use>
            </connection>
        </test_write>
        <!-- connection to read -->
       <test_read>
          <connection>
             <use>core_read</use>
          </connection>
       </test_read>
</resources>
 <!-- -/- -->

            <blocks> 
              <Banner>

              <class>Banner_Banner_Block</class>
             </Banner>

            </blocks>

     </global> 
</config>
  • can you post the config.xml file of your module? – Marius Sep 17 '14 at 6:43
3

You need to change this part of your config.xml:

<blocks> 
     <Banner>
         <class>Banner_Banner_Block</class>
     </Banner>
</blocks>

to

<blocks> 
     <banner><!-- lowercase letters here -->
         <class>Banner_Banner_Block</class>
     </banner>
</blocks>

then call the block like this:

echo $this->getLayout()->createBlock('banner/monblock')->setTemplate('banner/afficher.phtml')->toHtml();
1

In your config.xml file, you should have a code like this

FILE: app\code\local\Banner\Banner\etc\configx.xml

<global>
    <blocks>
        <banner> <!-- this is block alias name. VERY IMPORTANT ! -->
             <class>Banner_Banner_Block</class>
        </banner>
    </blocks>
</global> 

This defines your custom module block section. Without this, Magento will not understand where should it look for your custom blocks. So make sure, this code exist.

Kindly note the block alias name. It should be banner in this case. Then you can now refer your custom block like this.

 banner/monblock

Note all letters should be in small letter.

But I recommend you to change block alias name to banner_banner. That is the best practice. In general, it should be namespace_modulename. Now you can refer you block with

 banner_banner/monblock

In either way, you need to change this in everywhere. Means in layout file as well as in home.phtml file.

0

There issue with block type banner/Monblock .it should be in banner/monblock.Block class type should be small letter.

Also Suggest used Mage_Adminhtml_Block_Template class instead Mage_Core_Block_Template because Mage_Adminhtml_Block_Template is add more functions which will suitable with backend

The contend after </test_adminhtml_index_index> should with in handler

<reference name="content">
             <block type="banner/Monblock"  name="afficher_monbloc"
                  template="banner/afficher.phtml"/>
                   <block type="banner/adminhtml_grid" name="banner" />
      </reference>

class Banner_Banner_Block_Monblock extends Mage_Core_Block_Template change to

class Banner_Banner_Block_Monblock extends Mage_Adminhtml_Block_Template.
  • Not Working I am trying but same error – coder Sep 17 '14 at 5:36
  • I didnt get you – coder Sep 17 '14 at 5:48
  • can you tell layout file location ? – Amit Bera Sep 17 '14 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.