{%ITEMS%}"; public $itemTemplate = '
  • {%NICKNAME%}  {%RELATIVE_TIME%} ({%CREATED_AT%}) {%SAID%} :
    {%CONTENT%}
  • '; public function run() { $items = ""; $models = $this->getData(); foreach ($models as $model){ /** @var Comment $model */ $item = str_replace("{%HREF%}", Url::to(['article/view', 'id' => $model->aid, '#' => 'comment-' . $model->id]), $this->itemTemplate); $item = str_replace("{%AVATAR%}", Yii::$app->getRequest()->getBaseUrl() . "/static/images/comment-user-avatar.png", $item); $item = str_replace("{%NICKNAME%}", $model->nickname, $item); $item = str_replace("{%RELATIVE_TIME%}", Yii::$app->getFormatter()->asRelativeTime($model->created_at), $item); $item = str_replace("{%CREATED_AT%}", Yii::$app->getFormatter()->asTime($model->created_at), $item); $item = str_replace("{%SAID%}", " " . Yii::t('frontend', 'said'), $item); $item = str_replace("{%CONTENT%}", $model->content, $item); $items .= $item; } return str_replace("{%ITEMS%}", $items, $this->layout); } private function getData() { if( $this->data === null ){ /** @var CommentServiceInterface $commentService */ $commentService = \Yii::$app->get(CommentServiceInterface::ServiceName); $this->data = $commentService->getRecentComments(); } return $this->data; } }