[英]Single CSS from appasset.php per page in yii2
I am using the yii2
framework and the following code in appasset.php
: 我现在用的是
yii2
框架,并在下面的代码appasset.php
:
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [ 'css/site.css', 'css/country.css', 'css/admin/one.css', 'css/fg/two.css' ];
public $js = [ ];
public $depends = [ 'yii\web\YiiAsset', 'yii\bootstrap\BootstrapAsset', ];
My page pulls in all of the CSS files listed, but I only want one.css
in this page, and to use the other CSS files in other pages. 我的页面提取了列出的所有CSS文件,但我只希望此页面中的一个
one.css
,并使用其他页面中的其他CSS文件。
How can I prevent these other CSS files from appearing in this page? 如何防止其他CSS文件出现在此页面中? Or - is it possible to create a new
appasset.php
file for a single php
page? 或者-是否可以为单个
php
页面创建一个新的appasset.php
文件?
Each specific situation should have its own AssetBundle. 每个特定情况都应具有自己的AssetBundle。 You just create as many as you need and include them in the relevant view file:
您可以根据需要创建任意数量的对象,并将它们包括在相关的视图文件中:
Existing one: 现有的:
class AppAssets extends \yii\web\AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = ['css/site.css', 'css/country.css', 'css/fg/two.css'];
...
}
Additional one: 附加一:
class AdminAssetBundle extends \yii\web\AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = ['css/admin/one.css'];
public $depends = ['AppAssets'],
}
(Please add namespaces where necessary, I left those out) (请在必要时添加名称空间,我省略了这些名称空间)
As said: Then just include the one you need in the view that is relevant. 如前所述:然后在相关视图中包括您需要的那个。 ie.
即。 in your admin views you add:
AdminAsset::register($this);
在您的管理员视图中添加:
AdminAsset::register($this);
Because of the depends
, those views will automatically include your AppAssets
bundle. 由于
depends
,这些视图将自动包括您的AppAssets
捆绑包。
If your AppAssets
is the one from the yii app distribution (added by default), it is probably already being registered in the /views/layouts/main.php
-file. 如果您的
AppAssets
是AppAssets
应用程序发行AppAssets
中的一个(默认添加),则它可能已经在/views/layouts/main.php
-file中进行了注册。
That means that it is not required to be defined as a dependency. 这意味着不需要将其定义为依赖项。 I do consider it a good practice to keep the dependencies clear (if your admin one.css file actually depends on the ones from
AppAssets
, if not remove the $depends
alltogether). 我确实认为保持依赖关系清晰是一个好习惯(如果您的admin one.css文件实际上依赖于AppAssets的依赖
AppAssets
,如果未完全删除$depends
)。
Yii is smart enough to only include each asset bundle only once anyway. Yii非常聪明,无论如何只包含每个资产捆绑包一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.