Question

I have modules for category:

+modules/
 +category/
    +assets/
      +css/
      +js/
      +images/
    +components/
    +controllers/
    +models/
    +views/
    -CategoryModule.php

What is the best way to includes the css and jss to all views?

Was it helpful?

Solution

Publish and register in CategoryModule init method - this will make available your css and js in category module.
Something like this:

public function  init() {
    $path = $this->assetManager->publish(Yii::getPathOfAlias('application.modules.category.assets'));
    $this->clientScript->registerCssFile($path . '/css/some-css.css', 'screen, projection');
    $this->clientScript->registerScriptFile($path . '/js/some-js.js');
}

OTHER TIPS

create module layout file under views/layout and call it in module config file as

$this->layoutPath = Yii::getPathOfAlias('application.modules.moduleName.views.layouts');
        $this->layout = '/layouts/layoutname';

register all the js and css file as @PeterM mentioned

Create a folder "layouts" in your views folder. Create main.php in that layouts folder.

In your , add following code:

<link rel="stylesheet" href="<?php echo Yii::app()->request->baseUrl; ?>/css/style.css" type="text/css" media="screen"/>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/js/cycle.js"></script>

This is your default layout. So add,

<?php echo $content; ?>

This is another method to include css and js in all views.

Easy using this static method

<?php Yii::app()->clientScript->registerCssFile(Yii::getPathOfAlias('application.modules.curriculo.assets') . '/bootstrap/datepicker/css/datepicker.css'); ?>
<?php Yii::app()->clientScript->registerScriptFile(Yii::getPathOfAlias('application.modules.curriculo.assets') . '/bootstrap/datepicker/js/bootstrap-datepicker.js'); ?>

This should help you:

http://www.yiiframework.com/wiki/249/understanding-the-view-rendering-flow/

All three sections (beginContent, container and endContent) are explained in detail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top