Skip to content
Maciej Klemarczyk edited this page Apr 24, 2015 · 3 revisions

Usage widget in view

Widgets are primarily used in views. You can call the yii\base\Widget::widget() method to use a widget in a view. The method takes a configuration array for initializing the widget and returns the rendering result of the widget. For example, the following code inserts a redactor widget which is configured to use the Russian language, show source button and keep the input in the post_content attribute of $model.

<?php
use yii\redactor\widgets\Redactor;
?>
<?= Redactor::widget([
    'model' => $model,
    'attribute' => 'post_content',
    'clientOptions' => [
        'buttonSource' => true,
        'language' => 'ru',
    ],
]) ?>

Redactor can be also used with ActiveForm widget.

<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
use yii\redactor\widgets\Redactor;
?>

<?php $form = ActiveForm::begin(['id' => 'post-form']); ?>

    <?= $form->field($model, 'title') ?>

    <?= $form->field($model, 'post_content')->widget(Redactor::className(), [
        'clientOptions' => [
            'buttonSource' => true,
            'language' => 'ru',
        ],
    ]) ?>

    <div class="form-group">
        <?= Html::submitButton('Create') ?>
    </div>

<?php ActiveForm::end(); ?>

📘 More information about usage widgets: http://www.yiiframework.com/doc-2.0/guide-structure-widgets.html

Options

Widget client options are used to configure view and settings Redactor control.

Widget client option Type Description
buttonSource boolean This setting adds HTML code view button to the toolbar
fileUpload string Url to controller action responsible for upload and save files
imageManagerJson string Url to controller action responsible for image browsing
imageUpload string Url to controller action responsible for upload and save image files
language string This setting specify language of all toolbar labels

📘 More options on website: http://imperavi.com/redactor/docs/settings/

Clone this wiki locally