1

I want to add a block in left column of only my module, not whole website.

If I place it in <default> tag it will be shown in whole website, but I want to do something like <mymodule_default>, is it possible?

I know I can place my block in every layout_handler like:

 <mymodule_controllername_actionname>
    <reference name="left">
        <block type="mymodule/block" name="left_navigation" before="-" template="mymodule/left-nav.phtml" />
    </reference>
 </mymodule_controllername_actionname>

but this is not what I want, I want to do it as:

 <mymodule_default>
    <reference name="left">
        <block type="mymodule/block" name="left_navigation" before="-" template="mymodule/left-nav.phtml" />
    </reference>
 </mymodule_default>

Or is it not preferable?

Thanks

adrien54
  • 1,620
  • 1
  • 26
  • 31
Ahmed
  • 651
  • 17
  • 42

3 Answers3

1

You can set layout for your custom module only. For example you want a left-side column your layout file should be like this,

<?xml version="1.0"?>   
<layout version="0.1.0">   
  <modulename_index_index>   
    <reference name="root">   
      <action method="setTemplate"><template>page/2columns-left.phtml</template></action>   
    </reference>
    <reference name="left">   
      <block type="modulename/left" name="modulename_left" template="modulename/left.phtml"/>   
    </reference>   

  </modulename_index_index>   
</layout>   

And the page/2columns-left.phtml having the content of your left-side column.

Place this xml file in your app/design/frontend/default/THEME NAME/template/layout/module.xml

And dont forgot to tell about this to your module configuration file(config.xml). It should be like this,

<config>
...
<frontend>
<layout>
          <updates>
            <modulename>
              <file>modulename.xml</file>
            </modulename>
          </updates>
        </layout>
  ...
</frontend>
</config>

Update:

According to magento's layout rules , you can add <default> in your module. So it can set that block will be available for all pages(controllers) in that module. That's it. Let me know if you have any doubts.

Elavarasan
  • 2,579
  • 2
  • 25
  • 42
  • Yes but this way I have to write it in every layout handler, and if want want to extend my module with some other module and want to add any link to 1st module, it may not be possible this way – Ahmed Dec 26 '14 at 11:37
  • I'm sorry. I can't get you. – Elavarasan Dec 26 '14 at 11:39
  • make it simple, I don't want to write block in every layout handler, trying to find a better way – Ahmed Dec 26 '14 at 11:43
  • You mean like ` ` This way your left block will be visible on whole website not only in your module pages/controllers – Ahmed Dec 26 '14 at 12:40
1

There is a more elegant way to do this:

in your module layout.xml you can create a handle and just update it in every other handle, like this:

<lista_default>

<reference name="head">

<action method="setText">        
    <text>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.4/js/tether.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
    </text>   
</action>

</reference>

</lista_default>

<lista_index_index>

    <update handle="lista_default" />

    <reference name="content">

    </reference>

</lista_index_index>
Gabriel G
  • 97
  • 12
0

Got an answer on another website, copying it here :

You can only add layout update handle in each controller

public function preDispatch()
{
  parent::preDispatch();
  $this->getLayout()->getUpdate()
       ->addHandle('mymodule_default');
}

Or with instruction <update handle="mymodule_default"/> in all <mymodule_controllername_actionname> handles

Example:

<mymodule_controllername_actionname>
    <update handle="mymodule_default"/>
    ...
<mymodule_controllername_actionname>
Ahmed
  • 651
  • 17
  • 42