lawondyss/cdnloader

用于将 cdnjs.com 中的 CSS 和 JS 文件加载到网页中的工具。

dev-master 2015-01-09 14:10 UTC

This package is auto-updated.

Last update: 2024-09-17 22:09:24 UTC


README

用于将 cdnjs.com 中的 CSS 和 JS 文件加载到网页中的工具。

安装

通过 Composer

composer require lawondyss/cdnloader

示例

在 presenter 中控制工厂

protected function createComponentCdnLoader()
{
    $compiler = new CDNLoader\Compiler;

    // set directory for save remote files
    $compiler->setOutputDir('cdntemp');

    // set libraries
    $compiler->setLibrary(array(
            'name' => 'jquery', // WARNING! Name is name from cdnjs.cloudflare.com URL!
            'version' => '2.1.1',
            'files' => array( // all the files that you want
                'jquery.min.js',
                'jquery.min.map',
            ),
        ))
        ->setLibrary(array(
            'name' => 'jspdf',
            'version' => '0.9.0rc1',
            'min' => TRUE, // load file `jspdf.min.js`, if FALSE or without, then load file `jspdf.js`
        ));

    // or shortcut
    $libraries = array(
        array(
            'name' => 'jquery',
            'version' => '2.1.1',
            'files' => array(
                'jquery.min.js',
                'jquery.min.map',
            ),
        ),
        array(
            'name' => 'jspdf',
            'version' => '0.9.0rc1',
            'min' => TRUE,
        )
    );
    $compiler->setLibraries($libraries);

    return new CDNLoader\Loader($compiler);
}

在模板中控制

{control cdnLoader}

Nette 框架扩展示例

在 config.neon 中配置

extensions:
    cdnloader: CDNLoader\Extension

cdnloader:
    outputDir: cdntemp
    libraries:
        -
            name: jquery
            version: 2.1.1
            files:
                - jquery.min.js
                - jquery.min.map
        -
            name: jspdf
            version: 0.9.0rc1
            min: TRUE

在 presenter 中控制工厂

protected function createComponentCdnLoader()
{
    $factory = new CDNLoader\CDNLoaderFactory($this->context);
    return $factory->create();
}