Ahmed
Ahmed

Reputation: 651

Magento, default layout for the custom module

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

Upvotes: 1

Views: 1150

Answers (3)

Gabriel G
Gabriel G

Reputation: 121

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>

Upvotes: 1

Ahmed
Ahmed

Reputation: 651

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>

Upvotes: 0

Elavarasan
Elavarasan

Reputation: 2669

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.

Upvotes: 1

Related Questions