Search code examples
twitter-bootstrapyiiyii-extensions

Yii Bootstrap not loading JS files


I'm using Yii-Bootstrap in my brand new Yii project. I followed the install instructions exactly.

On the front page I am using the JS Carousel plugin.

$this->widget('bootstrap.widgets.TbCarousel', array(
    'items'=>array(
        array(
            'image'=>'http://placehold.it/770x400&text=First+thumbnail',
            'label'=>'asdf',
            'caption'=>'Cras justo odio, '
        ),
        array(
            'image'=>'http://placehold.it/770x400&text=Second+thumbnail',
            'label'=>'asdf',
            'caption'=>'Cras justo odio, '
        ),
        array(
            'image'=>'http://placehold.it/770x400&text=Third+thumbnail',
            'label'=>'asdf',
            'caption'=>'Cras justo odio, '
        ),
    ),
));

Uncaught TypeError: Object [object Object] has no method 'carousel'

This leads to a .carousel() not found error. It's like the JS files are not being registered. The CSS and everything looks fine.

I'm using the yii-environment extension..

return array(

    // Set yiiPath (relative to Environment.php)
    'yiiPath' => dirname(__FILE__) . '/../../../yii-1.1.13/framework/yii.php',
    'yiicPath' => dirname(__FILE__) . '/../../../yii-1.1.13/framework/yiic.php',
    'yiitPath' => dirname(__FILE__) . '/../../../yii-1.1.13/framework/yiit.php',

    // Static function Yii::setPathOfAlias()
    'yiiSetPathOfAlias' => array(
        // uncomment the following to define a path alias
        'bootstrap' => realpath(dirname(__FILE__).'/../extensions/bootstrap')
    ),

    // This is the main Web application configuration. Any writable
    // CWebApplication properties can be configured here.
    'configWeb' => array(

        'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
            'theme' => 'bootstrap',

        // Preloading 'log' component
        'preload' => array('log'),

        // Autoloading model and component classes
        'import' => array(
            'application.models.*',
            'application.components.*',
        ),

        // Application components
        'components' => array(
            'bootstrap'=>array(
                'class'=>'bootstrap.components.Bootstrap',
            ),

Any suggestions?


Solution

  • You will have to register the css and js specifically:

    Yii::app()->bootstrap->register();
    

    which is different from the way it was done in older versions of the extension. I do not know why this is not mentioned in the documentation anywhere.

    A good place to put the above line of code is in your layout, if your entire site/app uses yii-bootstrap, or if you are doing it in only specific views, you can put it in those views.

    P.S - You can find the function in the bootstrap/components/Bootstrap.php file.