antiphp / resource-loader
该软件包的最新版本(dev-master)没有可用的许可证信息。
Zend Framework (2) 视图助手,用于加载UI资源
dev-master
2015-03-04 23:01 UTC
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-15 00:49:11 UTC
README
这是一个Zend Framework (2) 视图助手,专注于依赖关系,用于加载CSS和JavaScript资源。
已完成的任务
-
资源加载器将请求的CSS和JavaScript资源注册到您的ZF head*()视图助手,并正确解决资源依赖关系。
例如,如果您使用Bootstrap,那么jQuery是Bootstrap所需的资源。因此,您需要定义两个资源,但您只需加载和考虑Bootstrap资源,因为在您的配置中已经配置了依赖关系(Bootstrap需要jQuery)。
未完成的任务
- 该软件包没有定义任何依赖关系,您需要自己定义所有资源依赖关系。
- 该软件包不会下载(服务器端)您定义或请求的任何库。
配置
以下是一个模块配置示例。首先,我们需要在视图助手服务配置中注册视图助手。
return array( 'view_helpers' => array( 'factories' => array( 'resource' => 'AntiPhp\ResourceLoader\View\Helper\ResourceLoaderServiceFactory' ) ) );
在同一配置文件中,我们需要定义我们的资源配置,如下所示
return aray( 'resource_loader' => array( 'resources' => array( 'jquery' => array( 'js' => 'vendor/jquery/jquery.min.js' ), 'bootstrap' => array( 'js' => 'vendor/bootstrap/js/bootstrap.min.js', 'css' => 'vendor/bootstrap/css/bootstrap.min.css', 'requires' => 'jquery' ), 'bootstrap.themed' => array( 'css' => 'vendor/bootstrap/css/bootstrap-theme.min.css', 'requires' => 'boostrap' ), 'yaml' => array( 'css' => array( 'vendor/yaml/css/flexible-columns.css', array('vendor/yaml/yaml/core/iehacks.css', 'lte IE 7') ), 'requires' => 'html5shiv' ), 'my_layout_1' => array( 'requires' => array( 'bootstrap.themed', 'font-awesome' ) ), 'my_layout_2' => array( 'requires' => 'yaml' ), // html5shiv, font-awesome, data-tables, ... ) ) );
用法
现在资源加载器知道所有UI资源,我们可以在基于Bootstrap的布局中使用它们。
$this->resource('my_layout_1'); // refers to configuration key resource_loader/resources/my_layout_1 ?> <html> <head> <?php $headLink = $this->headLink(); echo $headLink->toString(8), PHP_EOL; $headScript = $this->headScript(); echo $headScript->toString(8), PHP_EOL; ?> </head> <!-- .. --> </html>
或者我们可能更喜欢YAML而不是Bootstrap
$this->resource('my_layout_2'); // refers to configuration key resource_loader/resources/my_layout_2 ?> <html> <head> <?php $headLink = $this->headLink(); echo $headLink->toString(8), PHP_EOL; $headScript = $this->headScript(); echo $headScript->toString(8), PHP_EOL; ?> </head> <!-- .. --> </html>
或者在视图脚本中,我们希望使用jQuery DataTables
$this->resource('data-table'); ?> <table class="data-table"> <!-- .. --> </table>
资源加载器确保只加载所需的资源,并且您只需考虑一次依赖关系。