-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Nick Tsai | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,124 @@ | ||
# yii2-autoview-asset | ||
Yii2 auto allocating Asset for each View | ||
<p align="center"> | ||
<a href="https://github.com/yiisoft" target="_blank"> | ||
<img src="https://avatars0.githubusercontent.com/u/993323" height="100px"> | ||
</a> | ||
<h1 align="center">Yii 2 Auto Asset Bundle</h1> | ||
<br> | ||
</p> | ||
|
||
Base Asset Bundle for Yii2 to auto-allocate JS & CSS files for each View | ||
|
||
[![Latest Stable Version](https://poser.pugx.org/yidas/yii2-auto-asset-bundle/v/stable?format=flat-square)](https://packagist.org/packages/yidas/yii2-auto-asset-bundle) | ||
[![Latest Unstable Version](https://poser.pugx.org/yidas/yii2-auto-asset-bundle/v/unstable?format=flat-square)](https://packagist.org/packages/yidas/yii2-auto-asset-bundle) | ||
[![License](https://poser.pugx.org/yidas/yii2-auto-asset-bundle/license?format=flat-square)](https://packagist.org/packages/yidas/yii2-auto-asset-bundle) | ||
|
||
FEATURES | ||
-------- | ||
|
||
- *Each View would has it own **JS & CSS** asset files* | ||
|
||
- ***Standard Asset files' structure** same as View paths* | ||
|
||
- ***Single AssetBundle** automatically handles all Views' assets by registering* | ||
|
||
Felt dirty of writting *Javascript* code in View file? Got tired of creating asset for every View files? | ||
|
||
If each View need *Javascript* & *CSS* for it self, the AssetBundle library allocates pairs of asset files for each registered View, which each View's path is same as allocated assets path with base path setting. | ||
|
||
--- | ||
|
||
DEMONSTRATION | ||
------------- | ||
|
||
A View file which locates: | ||
|
||
``` | ||
yii2-app-basic/views/site/about.js | ||
``` | ||
|
||
After registering AutoAssetBundle, the View file would auto-load above assets files if exist: | ||
|
||
``` | ||
yii2-app-basic/web/dist/app/site/about.js | ||
yii2-app-basic/web/dist/app/site/about.css | ||
``` | ||
|
||
> `dist/app/` is the prefix base path which could be customized. | ||
--- | ||
|
||
REQUIREMENTS | ||
------------ | ||
|
||
This library requires the following: | ||
|
||
- PHP 5.4.0+ | ||
- Yii 2.0.0+ | ||
|
||
--- | ||
|
||
INSTALLATION | ||
------------ | ||
|
||
Install via Composer in your Yii2 project: | ||
|
||
``` | ||
composer require yidas/yii2-auto-asset-bundle | ||
``` | ||
|
||
--- | ||
|
||
CONFIGURATION | ||
------------- | ||
|
||
You could create an asset to extend `\yidas\web\AutoAssetBundle` with your application configuration: | ||
|
||
```php | ||
namespace app\assets; | ||
|
||
class AutoAssetBundle extends \yidas\web\AutoAssetBundle {} | ||
``` | ||
|
||
### Customized Setting | ||
|
||
You could customize asset to fit your application: | ||
|
||
```php | ||
namespace app\assets; | ||
|
||
class AutoAssetBundle extends \yidas\web\AutoAssetBundle | ||
{ | ||
// Base path & url for each view's asset in your application | ||
public $basePath = '@webroot/dist/app'; | ||
public $baseUrl = '@web/dist/app'; | ||
|
||
public $depends = [ | ||
'app\assets\AppAsset', | ||
]; | ||
} | ||
``` | ||
|
||
--- | ||
|
||
USAGE | ||
----- | ||
|
||
Register [configured asset](#configuration) in the content View, for example `yii2-app-basic/views/controller/action.php`: | ||
|
||
```php | ||
<?php | ||
|
||
\app\assets\AutoAssetBundle::register($this); | ||
``` | ||
|
||
After that, this view would auto load following files: | ||
|
||
``` | ||
yii2-app-basic/web/dist/app/controller/action.js | ||
yii2-app-basic/web/dist/app/controller/action.css | ||
``` | ||
|
||
The prefix path relates to the view path. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "yidas/yii2-auto-asset-bundle", | ||
"description": "Base Asset Bundle for Yii2 to auto-allocate JS & CSS files for each View", | ||
"keywords": ["yii2", "framework", "asset", "asset-bundle", "view"], | ||
"type": "yii2-extension", | ||
"license": "MIT", | ||
"support": { | ||
"issues": "https://github.com/yidas/yii2-auto-asset-bundle/issues?state=open", | ||
"source": "https://github.com/yidas/yii2-auto-asset-bundle" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Nick Tsai", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"yiisoft/yii2": "~2.0.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"yidas\\web\\": "src" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace yidas\web; | ||
|
||
use Yii; | ||
|
||
/** | ||
* Auto View Asset | ||
* | ||
* @author Nick Tsai <[email protected]> | ||
* @version 1.0.0 | ||
* @see https://github.com/yidas/yii2-auto-asset-bundle | ||
* @example | ||
* For asset file autoload, giving following setting: | ||
* $basePath = '@webroot/dist/app' & $baseUrl = '@web/dist/app' | ||
* For JS file case would be: | ||
* @app/views/site/index.php => @webroot/dist/app/site/index.js | ||
* @app/views/level/site/about.php => @webroot/dist/app/level/site/about.js | ||
*/ | ||
class AutoAssetBundle extends \yii\web\AssetBundle | ||
{ | ||
/** | ||
* Asset base setting | ||
* | ||
* @var string Path with default related path `dist/app` | ||
*/ | ||
public $basePath = '@webroot/dist/app'; | ||
public $baseUrl = '@web/dist/app'; | ||
|
||
/** | ||
* Asset initialization | ||
* | ||
* @return void | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
// Fetch View name | ||
$view = Yii::$app->controller->getView(); | ||
$viewFile = $view->getViewFile(); | ||
$viewFile = str_replace(Yii::$app->controller->getViewPath() . DIRECTORY_SEPARATOR, '', $viewFile); | ||
$viewName = str_replace("." . $view->defaultExtension, '', $viewFile); | ||
|
||
// Get full controller name (route) | ||
$controller = Yii::$app->controller->id; | ||
|
||
// Compose to a asset file name without extension | ||
$fileName = $controller . '/' . $viewName; | ||
|
||
// Define asset files with adding extension | ||
$jsFile = "{$fileName}.js"; | ||
$cssFile = "{$fileName}.css"; | ||
|
||
// Load asset files | ||
if (file_exists($this->basePath . '/' . $jsFile)) { | ||
$this->js[] = $jsFile; | ||
} | ||
if (file_exists($this->basePath . '/' . $cssFile)) { | ||
$this->css[] = $cssFile; | ||
} | ||
} | ||
} | ||
|