J
J
jehord2015-01-18 17:08:53
Yii
jehord, 2015-01-18 17:08:53

How to pass parameters to widget?

I have the following widget:

class Polls extends \yii\base\Widget {


  public function run() {
    $poll = Poll::findOne(20);
    $answers = Poll_choice::find()->where('poll_id = 20')->all();
    if($poll){
      if(!$poll->userCanVote(\Yii::$app->request->userIP)){
        $pollVote = new Poll_vote();
        if(Poll_choice::issetChoiceInPoll($poll->id, 56)){
          $pollVote->poll_id = $poll->id;
          $pollVote->choice_id = 56;
          $pollVote->user_id = \Yii::$app->user->id;
          $pollVote->ip_address = \Yii::$app->request->userIP;
        $pollVote->save();
        }else
          echo "Test";
      }
    }
  return $this->render('poll.twig',[
    'question' => $poll,
    'answers' => $answers,
  ]);
  }
}

At the moment, static parameters are passed in the model.
The work is done with the twig template engine. I just can’t figure out how to transfer data from the page to this widget, for example, using the post method.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
developinwed, 2015-01-19
@developinwed

Add property to widget like message

class Polls extends \yii\base\Widget {
    public $message;
    ...
}

Then, to pass the $message parameter to the widget, it is enough to use the code:
<?php
use app\components\Polls;
?>
<?= Polls::widget(['message' => ' Yii2.0']) ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question