0

I'm writing an extension for the community and I need it to inject an html tag to the head, which is not constant, eg:

<script>var something = "this value comes from php code";</script>

what's the best way to inject such a tag, to all site pages, without require the admin to do any additional config on the admin panel?

thanks!

EDIT:

Based on Bernhard Prange's answer, here's the solution for future reference (note - it just do alert don't actually inject any meaningful javascript, but you can easily put anything in the template):

app\design\frontend\base\default\template\page\html\inject_js.phtml

<script>alert("TEST")</script>

app\design\frontend\base\default\layout\test.xml

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
    <default>
        <reference name="head">

                <block type="page/html" name="dynamicJs" as="dynamicJs" template="page/html/inject_js.phtml" />

        </reference>
    </default>
</layout>

app\code\community\SomeName\Test\etc\config.xml

  <!-- note, DON'T put this inside the <global> tag! -->
    <frontend>
      <layout>
        <updates>
          <test>
            <file>test.xml</file>
          </test>
        </updates>
      </layout>
    </frontend>
  • I don't see it. Sorry. To exclude mistakes fom the module or config.xml, put the reference tag within your theme's layout.xml. There it should work. If yes, you know where to look deeper into. Try what Milan says. That is often one of my mistakes ;) – Bernhard Prange Oct 18 '15 at 18:05
  • ah-ha found it! the <frontend> shouldn't be inside global! thanks mate, you helped a lot! – Ronen Ness Oct 19 '15 at 9:13
1
  • make a .phtml file it which you put the content above.
  • add the phtml file in you extension layout in the defaultblock with reference name="head"

open a File in app/design/frontend/base/default/layout/myExtensionLayout.xml

<layout version="0.1.0">    
      <default translate="label" module="page">
            <reference name="head">
              <block type="core/template" name="dynamicJs" as="dynamicJs" template="page/html/dynamicJs.phtml" />
               </reference>
      <default>

Within app/design/frontend/base/default/template/page/html/dynamicJs.phtml you do you thing. And add this file with it's folder structure to your extension.

Sorry to be so short but I have to walk the dog.

  • could you give example / be more specific? – Ronen Ness Oct 18 '15 at 13:41
  • hi Bernhard Prange thank you for the update, it was helpful. I tried to do what you said but it doesn't seem to work. do you see anything I did wrong? (added the code to the original question). thanks :) – Ronen Ness Oct 18 '15 at 14:41
  • Change the block type from page/html to core/template. Then it should work – Milan Simek Oct 18 '15 at 17:49
  • thanks Milan, but it wasn't the problem. in the config.xml I put the <frontend> inside <global>, which was the mistake. – Ronen Ness Oct 19 '15 at 9:14

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.