2017 © Pedro Peláez
 

yii2-component yii2-add-body-class

image

garazol/yii2-add-body-class

  • Monday, March 5, 2018
  • by garazol
  • Repository
  • 1 Watchers
  • 1 Stars
  • 119 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 35 % Grown

The README.md

yii2-add-body-class

This Yii2 behavior class allows us to assign CSS classes to the layout HTML body element from controllers., (*1)

1. Download

Yii2-add-body-class can be installed using composer. Run following command to download and install Yii2-add-body-class:, (*2)

composer require garazol/yii2-add-body-class

2. Configure

In the base controller place the behavior definition:, (*3)

(For detailed examination of special parameters see comments in this block.), (*4)

// 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,
        ],
    ];
}

In the layout template you can render the composed body classes:, (*5)

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

In any controller (which is extended from the base controller and inherits the behavior) you can add extra classes to body easily, just use:, (*6)

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']);
}

For real example see this example code., (*7)

The Versions

05/03 2018

v1.0

1.0.0.0

  Sources   Download

08/03 2017

dev-master

9999999-dev

YII2 component which allows us to assign CSS classes to HTML body element from controllers easily

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar garazol

yii2 yii2-add-body-class garazol

08/03 2017

dev-stable

dev-stable

YII2 component which allows us to assign CSS classes to HTML body element from controllers easily

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar garazol

yii2 yii2-add-body-class garazol

08/03 2017

v1.3

1.3.0.0

YII2 component which allows us to assign CSS classes to HTML body element from controllers easily

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar garazol

yii2 yii2-add-body-class garazol

08/03 2017

v1.2

1.2.0.0

YII2 component which allows us to assign CSS classes to HTML body element from controllers easily

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar garazol

yii2 yii2-add-body-class garazol

08/03 2017

v1.1

1.1.0.0

YII2 component which allows us to assign CSS classes to HTML body element from controllers easily

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar garazol

yii2 yii2-add-body-class garazol

07/03 2017

1.0.0

1.0.0.0

YII2 component which allows us to assign CSS classes to HTML body element easily

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar garazol

yii2 yii2-add-body-class garazol