-1

I try to add the following template on all pages in the header by using reference. I tried it like this:

app/design/frontend/venedor/default/template/black/alert/index.phtml:

<script>alert("Hello world!")</script>

app/design/frontend/venedor/default/layout/alert.xml:

<?xml version="1.0"?>

<layout version="0.1.0">
    <default>
        <reference name="header">
            <block type="core/template" name="newsletter_alert" as="newsletter_alert" template="black/alert/index.phtml" />
        </reference>
    </default>
</layout>

But no alert is shown. Why does it not work?


I know I can add my block as child block to <block type="page/html_header" name="header" as="header"> in app/design/frontend/{{packageName}}/default/layout/page.xml. I already tried this and it works.

But my question is about how to do solve this by using the XML tag reference from another layout file e.g. from (app/design/frontend/venedor/default/layout/alert.xml) .


  • I think it should be header. – kunj Jun 1 '18 at 10:21
  • @kunj, no and it makes no difference. I just tried it. – Black Jun 1 '18 at 10:24
  • but head for page title, script, css and meta tag. – kunj Jun 1 '18 at 10:27
  • Yes, but my template is only a script tag? So head should be fine or not? – Black Jun 1 '18 at 10:33
  • I think you are tried in magento2? – Sathish Jun 1 '18 at 10:34
1

TRy below code.

<default>
    <reference name="header">
        <reference name="top.container">
            <block type="core/template" name="newsletter_alert"  template="black/alert/index.phtml" />
        </reference>
    </reference>
<default>

Open header.phtml And Put this code before div.

<div class="header-language-background">
<?php echo $this->getChildHtml('newsletter_alert') ?>
  • 1
    Thanks for your alternative solution. But I don't understand how this answers my question which is about using reference to solve this. – Black Jun 1 '18 at 10:59
  • I edit code try above – Jugal Kishor Jun 1 '18 at 12:22
  • try via local.xml – Jugal Kishor Jun 1 '18 at 12:22
  • Putting the reference code in app/design/frontend/{{package}}/default/layout/layout.xml made it work! But I am wondering why does it not work with my custom .xml file app/design/frontend/{{package}}/default/layout/alert.xml? Maybe it does only work in combination with a custom module? If you can answer this then I can finish this topic and accept your solution. – Black Jun 1 '18 at 12:27
  • make a local.xml file in your custom theme and put code i suggest app/design/frontend/{{package}}/default/layout/local.xml – Jugal Kishor Jun 1 '18 at 12:35
0

TRy below code.

<default> 
       <reference name="after_body_start">
                <block type="core/template" name="newsletter_alert" as="newsletter_alert" template="black/alert/index.phtml" />
       </reference> 
<default> 
  • Does not work either. – Black Jun 1 '18 at 12:20
  • @Black, answer upadted try now – Chirag Rajput Jun 1 '18 at 13:07
0

Try this :

<?xml version="1.0"?>

<layout version="0.1.0">
    <default>
        <reference name="header">
            <block type="core/template" name="newsletter_alert"  template="black/alert/index.phtml" />
        </reference>
    </default>
</layout>

And add this code:

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

in

app/design/frontend/{{theme package name}}/{{theme name}}/template/page/html/header.phtml

If it is not there then copy and paste in theme.

  • I tried it, but it does still not work. No alert shows. – Black Jun 1 '18 at 10:29
  • add your XML code in local.xml. – kunj Jun 1 '18 at 10:38
  • Yes, I know I can add my block as child block to <block type="page/html_header" name="header" as="header"> but my question is about how to do this by using reference from another layout file. – Black Jun 1 '18 at 10:40

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.