I would recommend the following approach for adding third party scripts in Magento:
1) Open your theme layout update file(preferably local.xml)
2) Add the following xml code:
<default>
<reference name="after_body_start">
<block type="core/template" name="after_body_start_script" template="page/html/scripts/after_body_start.phtml" />
</reference>
<reference name="before_body_end">
<block type="core/template" name="before_body_end_script" template="page/html/scripts/before_body_end.phtml" />
</reference>
</default>
3) Now you can add the relevant scripts to the the files page/html/scripts/after_body_start.phtml
or
page/html/scripts/before_body_end.phtml
So basically the addBodyClass
method replaces some characters with -
on the fly using regular expression:
public function addBodyClass($className)
{
$className = preg_replace('#[^a-z0-9]+#', '-', strtolower($className));
$this->setBodyClass($this->getBodyClass() . ' ' . $className);
return $this;
}
As you can see, when this method is called the original body classes are updated and the extra body class is added with a space between the original classes. So in order to add several classes I suggest you do the following in your xml file:
<action method="addBodyClass">
<className>classname1</className>
</action>
<action method="addBodyClass">
<className>classname2</className>
</action>
<action method="addBodyClass">
<className>classname3</className>
</action>
If you really want to be able to add several classes to the body using that method, then you will need to create a custom module that extends that method:
config.xml
<?xml version="1.0"?>
<config>
<modules>
<DigitalPianism_Multiplebodyclass>
<version>0.0.1</version>
</DigitalPianism_Multiplebodyclass>
</modules>
<global>
<blocks>
<page>
<rewrite>
<html>DigitalPianism_Multiplebodyclass_Block_Page_Html</html>
</rewrite>
</page>
</blocks>
</global>
</config>
Block/Page/Html.php
<?php
class DigitalPianism_Multiplebodyclass_Block_Page_Html extends Mage_Page_Block_Html
{
public function addBodyClass($classNames)
{
if (is_array($classNames))
{
foreach($classNames as $className)
{
parent::addBodyClass($className);
}
}
else parent::addBodyClass($classNames);
return $this;
}
}
Then in your custom helper you will be able to do:
public function addClasses() {
return array('classname1','classname2','classname3');
}
Best Answer
You could try adding a new structural block and echo this out before the tag where you require.
You would then need to echo out this new structural block in your page templates before the body tag