PHP code example of garazol / yii2-add-body-class

1. Go to this page and download the library: Download garazol/yii2-add-body-class library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

garazol / yii2-add-body-class example snippets


// reference to the behavior class:
use garazol\yii2AddBodyClass\components\behaviors\BodyClassBehavior;

// add body class behavior configuration to the behaviors method:
/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        'BodyClassBehavior' => [
            'class' => BodyClassBehavior::className(),
            // you can define here what kind of classes should be rendered automatically
            // key: AUTO_GENERATE_USER_LOGGED_STATUS can provide user-logged-in or user-logged-out
            // key: AUTO_GENERATE_CONTROLLER_ACTION can provide the current controller and action name classess, e.g.: controller-site and action-index
            // key: AUTO_GENERATE_MODULE can provide the current module, e.g.: module-example
            'autoGeneratedClassTypes' => [
                BodyClassBehavior::AUTO_GENERATE_USER_LOGGED_STATUS,
                BodyClassBehavior::AUTO_GENERATE_CONTROLLER_ACTION,
                BodyClassBehavior::AUTO_GENERATE_MODULE,
            ],
            // you can control the class style 
            // for hyphen separated classes use this (default) (results: user-logged-in)
            'classStyle'=>BodyClassBehavior::CLASS_STYLE_HYPHEN,//default
            // for camel case classes use this constant (results: userLoggedIn)
            //'classStyle'=>BodyClassBehavior::CLASS_STYLE_CAMEL_CASE,
        ],
    ];
}

<body class="<?= Html::encode($this->context->renderBodyClasses()); 

public function actionExample()
{
    // code here...
    $this->addBodyClass('example');
    // or multiple classes
    $this->addBodyClass(['example', 'something']);
    // remove addded class
    $this->removeBodyClass('example');
    //remove multiple classes
    $this->removeBodyClass(['example', 'something']);
}