Метод 1 - layout.xml:
a. For files you want to include on every page
For css or js files you want to add to every page, you edit the page.xml files located in your layout folder (app/design/frontend/default/your_theme/layout/page.xml). Within this file, at the top you will find the <default> area with the **<block name="root" … >**. This block has a child named head which contains the included css and js elements.
<block type="page/html_head" name="head" as="head">
<action method="addJs"><script>prototype/prototype.js</script></action>
<action method="addJs" ifconfig="dev/js/deprecation"><script>prototype/deprecation.js</script></action>
<action method="addJs"><script>prototype/validation.js</script></action>
<action method="addJs"><script>scriptaculous/builder.js</script></action>
...
</block>
Здесь вы можете добавить свой javascript и css.Обратите внимание, что все добавляемые вами Js-файлы относятся к папке «js» в корневом каталоге.Файлы css включены в файлы скина текущих и стандартных шаблонов (skin / frontend / default / your_template (& default) / css).
b. Specific areas
If you want to include css or js on only certain areas (such as the checkout process or catalog pages), you can do that also. For instance, if you want it in a single product view (product view vs product list), you can open up catalog.xml, find <catalog_product_view> area (near line 168 in vs 1.2.1). Add your code in there – notice that we are using the <reference> tag rather than <block> tags. We use the “reference” tag to reference other blocks that are in other areas of the template.
<reference name="head">
<action method="addJs"><script>varien/product.js</script></action>
<action method="addItem"><type>js_css</type><name>calendar/calendar-win2k-1.css</name><params/><!--<if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/calendar.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/lang/calendar-en.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
<action method="addItem"><type>js</type><name>calendar/calendar-setup.js</name><!--<params/><if/><condition>can_load_calendar_js</condition>--></action>
</reference>
Использование также можно использовать в макетеОбласти XML в административной части (страницы CMS, категории и дизайн продуктов).Это может выполнить то же самое, а также добавить или удалить другие блоки.
Метод 2 - Код блока:
Мы можем выполнить все это также в коде,Эти функции определены в Mage_Page_Block_Html_Head.Таким образом, мы можем использовать этот код в классе блока (а не в файле .phtml!):
$headBlock = $this->getLayout()->getBlock('head');
$headBlock->addJs('somefolder/yay.js');
Я предлагаю просматривать файлы page.xml до тех пор, пока не найден синтаксис removeItem ($ type,$ name для метода, для xml), который будет очень удобен для вас для добавления или удаления ресурсов по мере необходимости!
<action method="removeItem"><type>js</type><name>calendar/calendar.js</name</action>
$this->getLayout->getBlock('head')->removeItem('js', 'calendar/calendar.js');
Статья была опубликована: http://www.exploremagento.com/magento/adding-and-remove-js-css.php