1

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?

1
  • create the header page and call it in all pages Commented Apr 20, 2013 at 9:07

5 Answers 5

3

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');
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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.

2 Comments

what is the $content and where should I add this.
Add $content in your main.php only. When you use layout view like layout/main.php or layout/column1.php etc the $content variable contains the view content (controller/action render) and then passing into the layout.
-1

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'); ?>

Comments

-2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.