skip to Main Content

I have the function displayName in php that returns a string like this:

"${name} <i class="fa fa-check-circle" style="font-size:22px;color:green"/>"
public function getDisplayName()
{
    /** @var Module $module */
    $module = Yii::$app->getModule('user');

    if ($module->displayNameCallback !== null) {
        return call_user_func($module->displayNameCallback, $this);
    }

    $name = '';

    $format = Yii::$app->settings->get('displayNameFormat');

    if ($this->profile !== null && $format == '{profile.firstname} {profile.lastname}') {
        $name = $this->profile->firstname . ' ' . $this->profile->lastname;          
        if (($this->profile->user_id == 1)){
            $check_mark = '<i class="fa fa-check-circle" style="font-size:22px;color:green"/>';
        }
        else{
            $check_mark = '';   
        }
    }

    // Return always username as fallback
    if ($name == '' || $name == ' ') {
        return $this->username;
    }

    return $name . ' '.  $check_mark;
}

I call that function in my component like this:

public function run() {
  return $this->render('containerProfileHeader', [ 'title' => $this->title, ]);
}
    
<h1 class="<?= $classPrefix ?>"><?= Html::encode($title) ?></h1>
    
public function init() {
  parent::init();
  $this->title = $this->container->getDisplayName();
}

The expectation was having the string returned by getDisplayName() rendered in the html as is but instead I’ve got this:

Output Screenshot

2

Answers


  1. Chosen as BEST ANSWER

    Issue resolved from

    <?= Yii::$app->formatter->asHtml($title); ?>
    
    

    Used

    Yii::$app->formatter->asHtml instead of Html::encode


  2. You should try echo() functions in the return function. I hope it will work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search