0

I'm having a bit of trouble with a custom layout block which I'm trying to add to the head.phtml file of my theme. Basically, I'm trying to add extra favicon definitions to the header of my document.

In my local.xml file, I've added the lines:

<default>
    <reference name="head">
        <block type="core/template" name="favicon.extra" as="favicon.extra" template="page/html/head/favicon-extra.phtml" />
    </reference>
</default>

And then inside head.phtml, I'm calling this as a childHtml template as:

<link rel="icon" href="<?php echo $this->getFaviconFile(); ?>" type="image/x-icon" />
<link rel="shortcut icon" href="<?php echo $this->getFaviconFile(); ?>" type="image/x-icon" />
<?php echo $this->getChildHtml('favicon.extra') ?>

The problem is that the block is rendered twice, in two different spots:

  1. Underneath the other favicons, as per the head.phtml getChildHtml call, and
  2. After the other scripts in the header

The desired outcome is that I only want the first of these two to render.

From what I've read, I think this has to do with that particular block/reference being automatically rendered via the toHtml output - is this correct?

If so, is there a better/correct way to accomplish what I'm trying to do here? Is there another way to remove the second render?

Any help possible is much appreciated.

Note: apologies if this is considered bad practice but I'm reposting a question I posted on StackOverflow (https://stackoverflow.com/questions/25348878/layout-xml-block-rendered-twice-in-magento-skin) as I thought this might be a more appropriate forum.

3

You don't need the line

<?php echo $this->getChildHtml('favicon.extra') ?>

in head.phtml.
There is already this line that renders all child blocks

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

[EDIT]
If for some reason you want to add the block in a specific place, you can use this:

<?php echo $this->getChildHtml('favicon.extra') ?>
<?php this->unsetChild('favicon.extra');?>
  • The problem with this is that I want the block rendered in the position defined by the position of my output line in head.phtml. Removing this line does remove the duplicate block output, but it also means the block isn't rendered in the right location. – fvzzy Aug 19 '14 at 13:10
  • @fvzzy I don't know why would you want that. It's the <head> tag. But I'm sure you have a reason. You can try reverting to your original code and after the line that renders your block add this $this->unsetChild('favicon.extra') – Marius Aug 19 '14 at 13:17
  • Seems a bit counterintuitive but whatever - totally worked. Champion. Thanks! – fvzzy Aug 19 '14 at 13:38
  • @fvzzy. Glad it works. I added it to the answer also. – Marius Aug 19 '14 at 13:40
0

One solution is to remove the xml code from local.xml and instead of

<?php echo $this->getChildHtml('favicon.extra') ?>

in the header.phtml , use following code:

<?php  echo $this->getLayout()->createBlock('core/template')->setTemplate('page/html/head/favicon-extra.phtml'); ?>
  • Seems like an elegant solution but didn't have any luck getting this to work unfortunately. Doesn't seem to render anything in that spot at all. – fvzzy Aug 19 '14 at 13:39
  • are you still struggling, Marius answer seems a better solution. – MTM Aug 19 '14 at 13:43

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.