yii\widgets\DetailView::renderAttribute PHP Method

renderAttribute() protected method

Renders a single attribute.
protected renderAttribute ( array $attribute, integer $index ) : string
$attribute array the specification of the attribute to be rendered.
$index integer the zero-based index of the attribute in the [[attributes]] array
return string the rendering result
    protected function renderAttribute($attribute, $index)
    {
        if (is_string($this->template)) {
            $captionOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'captionOptions', []));
            $contentOptions = Html::renderTagAttributes(ArrayHelper::getValue($attribute, 'contentOptions', []));
            return strtr($this->template, ['{label}' => $attribute['label'], '{value}' => $this->formatter->format($attribute['value'], $attribute['format']), '{captionOptions}' => $captionOptions, '{contentOptions}' => $contentOptions]);
        } else {
            return call_user_func($this->template, $attribute, $index, $this);
        }
    }

Usage Example

 /**
  * @inheritdoc
  */
 protected function renderAttribute($attribute, $index)
 {
     if ($attribute['value'] instanceof Closure) {
         $attribute['value'] = call_user_func($attribute['value'], $this->model, $index);
     }
     return parent::renderAttribute($attribute, $index);
 }