如何在yii2中将数据从模型传递到视图

如何在yii2中将数据从模型传递到视图,yii2,Yii2,在这里,我试图设计一个工资单。我想在文本旁边传递模型中的数据。像- <p class="text-center">Pay Slip for the month of </p> _printSalarystatement.php <?php use yii\helpers\Html; use yii\widgets\DetailView; /* @var $this yii\web\View */ /* @var $model frontend\modules

在这里,我试图设计一个工资单。我想在文本旁边传递模型中的数据。像-

<p class="text-center">Pay Slip for the month of  </p>
_printSalarystatement.php

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model frontend\modules\salary\models\Salary */
//@var $model yii\base\Model
//@var $totaldays any

//$this->title = $model->s_id;
$this->params['breadcrumbs'][] = ['label' => 'Salaries', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="salary-view">

    <h1><?= Html::encode($this->title) ?></h1>
    <h1><strong><p class="text-center">PS Enterprise</p></strong></h1>
    <p class="text-center">Pay Slip for the month of s_period </p>


    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            's_id',
            's_date',
            's_period',
            's_empid',
            's_empname',
            's_workingdays',
            's_leave',
            's_holiday',
            's_wageperday',
            's_totalwage',
            's_ovthour',
            's_ovtrateperhour',
            's_ovtamount',
            's_tiffinworkingday',
            's_tiffinrateperday',
            's_wdtiffinamount',
            's_sundayworked',
            's_sundayrate',
            's_sundaytiffinamount',
            's_nightcount',
            's_nightallrate',
            's_nightallowance',
            's_convday',
            's_convrate',
            's_conveyanceamount',
            's_tiffinovtcount',
            's_tiffinovtrate',
            's_tiffinovtamount',
            's_incentivecount',
            's_incentiverate',
            's_incentive',
            's_totalearning',
            's_epf',
            's_esi',
            's_ptax',
            's_takehome',
        ],
    ]) ?>

</div>

PS Enterprise

s\U期间当月的工资单

通过控制器操作,我得到以下屏幕


我想要的是通过detailview中显示在“月工资单”行之后的期间。我的问题是如何做

您需要将数据从
控制器
操作
传递到您正在渲染的
视图

e、 g希望您希望将
$name
变量从索引操作发送到视图

行动就像

public function actionIndex()
    {
        return $this->render('index', ['name' => 'Tanmay']);
    }
现在,在
render
函数的第二个参数中传递的数组就是变量列表。在索引视图中,所有键都将被视为变量

索引视图的代码将是

<h1>Welcome <?= $name; ?>
内部控制器

鉴于:

<p class="text-center">Pay Slip for the month of <?= $s_period; ?> </p>

月工资单


Hi Unknown,我尝试了您的解决方案,但结果是变量未定义。我将添加我的控制器操作和视图文件以使其清晰。请让我知道该怎么做。感谢更新问题,同时添加您得到的错误我已经添加了我的当前位置以及我在问题中尝试执行的操作。获取错误-未定义变量:控制器操作中的s_period。您必须在控制器中初始化$s_period变量,并为其赋值
$content = $this->renderPartial('_printSalarystatement', [
'model' => $model,
'dataProvider' => $dataProvider,
'searchModel'  => $searchModel,
'data'=> $data,
's_period' => $s_period // new variable added
]
);
<p class="text-center">Pay Slip for the month of <?= $s_period; ?> </p>