1

I've created a module with a block that extends Mage_Page_Block_Html_Head but when I add it to the original head block, it duplactes almost every meta tag like <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> and <meta name="robots" content="NOINDEX,NOFOLLOW" /> and so on.

I just added an item via layout.xml to that block and only that item is rendered in the getCssJsHtml function of my block. But I don't want the rest of the original head block being rendered twice.

Can somebody tell me how I can get rid of that?

Here is my block code:

class Mr_Global_Block_Head extends Mage_Page_Block_Html_Head
{
    /*
     * override getCssJsHtml, so it doesn't throw an error when no item is added to the block
     */
    public function getCssJsHtml()
    {
        if (!isset($this->_data['items']) || !count($this->_data['items']))
            return '';
        return parent::getCssJsHtml();
    }
}

Here is the initial block xml:

<default>
    <reference name="head">
        <block type="mr_global/head" name="mr.head.scripts"></block>
    </reference>
</default>

And here I add some script to the block (in another xml file):

<default>
    <reference name="head">
        <reference name="mr.head.scripts">
            <action method="addItem">
                <type>js</type>
                <name>mediarocks/yags/jquery-yags.js</name>
            </action>
        </reference>
    </reference>
</default>

My source problem, I want to solve is the following: I want to add external js too with the addText function that needs a seperate block that is then rendered by the getChildHtml() from the original head block (like described here: https://stackoverflow.com/questions/4654822/adding-external-javascript-file-to-magento#answer-11674174), which is after the getCssJsHtml() function. But I want to make sure that the external js files are added before my internal js files, so I thought I can simply add another head block in the original head block, that is also rendered by the getChildHtml() function..

Thanks in advance!

3
  • some code would be great, how do you include your block etc. Nov 15 '13 at 20:01
  • sry haven't been there during the weekend but finally added some code, hope it helps! thanks again Nov 18 '13 at 9:48
  • glad you have is sorted. you can post an answer to your own question and accept it. Nov 18 '13 at 19:40
1

The reason you get everything twice is because you now have two head blocks in your layout, the core one and your new one. My suggestions would be.

  1. If you just want to change the method getCssJsHtml then I would suggest rewriting the block in your config.xml rather than trying to change the layout.
  2. If you just want to add new js and css the just use <reference name="head"><action method="addJs"><script>new/file.js</script></action></reference>
2
  • Thanks David but thats no solution for me, I added my source problem to my original question.. But I think its better to extend the Head Block to be able to add external JS files without a block.. Nov 18 '13 at 10:32
  • @silenzium you could use my 1st suggestion and rewrite the Mage_Page_Block_Html_Head. You then just need to create a metod that will add the external file to the beginning of the items array. Nov 18 '13 at 12:26
1

You add your block here :

<default>
   <reference name="head">
       <block type="mr_global/head" name="mr.head.scripts"></block>
   </reference>
</default>

But did you remove the other one ?

It seems that you have the page/html_head then your block. So there is duplication. You need the remove instruction on the other block ?

1
  • 1
    man! I don't know if I understood exactly what you meant but you've just led me in the right direction... I just had to change the template and remove the stuff in there... omg me so stupido... Nov 18 '13 at 17:47
0

I think this is due to extending the Block instead of override. When mentioned

<block type="mr_global/head" name="mr.head.scripts"></block>

it may calling some default functions in parent class.

Change the block type as type="mr_global" which are using default head type in .xml files.

thank you,

Nandu1

0

THE ANSWER:

// In my Head block:
protected function _construct()
{
    $this->setTemplate('mr/global/head.phtml');
}

template file:

<?php echo $this->getCssJsHtml() ?>
<?php echo $this->getChildHtml() ?>

Sorry for bothering you and thank you all again for the help! It was too simple for me to realize... :)

(btw: I would upvote all you guys but can't due to my low reputation..)

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.