简体   繁体   English

Magento-动态加载CSS和JS

[英]Magento - Load dynamically CSS and JS

we want to load css and javascript files dynamically for the pages in our magento system. 我们想要为magento系统中的页面动态加载CSS和javascript文件。 By reason of growing js and css file we want to split them in seperate files and load them for the current page. 由于js和css文件的增加,我们希望将它们拆分为单独的文件,并为当前页面加载它们。 We use the CMS Advanced content manager for managing our page content. 我们使用CMS Advanced内容管理器来管理页面内容。 Cause we have something like contenttypes in the CMS, we thought to figure out which contenttype the current page has. 因为我们在CMS中有类似contenttype的内容,所以我们想找出当前页面具有哪种contenttype。 Referring of the type we thought to load the css and js file by the name of the type (cause the typename is an alias and unique in the system). 引用该类型时,我们认为是通过该类型的名称来加载css和js文件的(因为typename是别名,在系统中是唯一的)。 Cause i am not to deep in magento coding i have no clue where i should start. 因为我对magento编码不了解,所以我不知道应该从哪里开始。

But maybe there is another solution or a known solution to achieve what we want. 但是也许有另一种解决方案或已知的解决方案可以实现我们想要的目标。

Have you tried using XML to load JS or CSS on particular content pages? 您是否尝试过使用XML在特定内容页面上加载JS或CSS?

Here is an example of loading CSS & JS file. 这是加载CSS和JS文件的示例。

Content Page => Design Tab => Custom Layout Update XML. 内容页面=>设计选项卡=>自定义布局更新XML。

<reference name="head">
    <action method="addItem"><type>skin_css</type><name>css/your_css.css</name></action>
    <action method="addItem"><type>skin_js</type><name>js/your_js.js</name></action>
</reference>

You could create an extension that observes the 'layout load before' event. 您可以创建一个扩展来观察“布局加载之前”事件。 With some request params you could identify the pages where you want to include some css or js. 使用一些请求参数,您可以标识要包含某些CSS或JS的页面。

For Example: 例如:

app/code/local/Foo/Bar/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Foo_Bar>
            <version>0.1.0</version>
        </Foo_Bar>
    </modules>
    <global>
        <models>
          <foo_bar>
            <class>Foo_Bar_Model</class>
          </foo_bar>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <customer_is_logged_in_observer>
                        <class>foo_bar/observer</class>
                        <method>beforeLoadLayout</method>
                    </customer_is_logged_in_observer>
                </observers>
            </controller_action_layout_load_before>
        </events>
     </frontend>
</config>

app/code/local/Foo/Bar/Model/Observer.php

class Foo_Bar_Model_Observer
{
    public function beforeLoadLayout($observer)
    {   
        if(Mage::app()->getRequest()->getControllerName()=='page' && 
            Mage::app()->getRequest()->getRouteName()='cms')
        {
            $head=$observer->getEvent()->getLayout()->getBlock('head');
            $head->addItem('skin_js', 'js/foo.js');
            $head->addItem('skin_css', 'css/foo.css');
        }

    }
}

Just check out the module documentation, a dynamic layout is implemented. 只需查看模块文档,即可实现动态布局。 So you can add a specific layout for a certain content type: 因此,您可以为特定的内容类型添加特定的布局:

acm for magento 1.x: (end of page) https://www.advancedcontentmanager.com/documentation/content/php-helper-methods-render-methods magento 1.x的ACM:(页面末尾) https://www.advancedcontentmanager.com/documentation/content/php-helper-methods-render-methods

acm for magento 2.x: https://www.advancedcontentmanager.com/documentation/m2/developers/dynamic-layout-handle 适用于magento 2.x的ACM: https ://www.advancedcontentmanager.com/documentation/m2/developers/dynamic-layout-handle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM