Answer the question
In order to leave comments, you need to log in
How is it more elegant and architecturally correct to implement hiding / displaying view elements using yii?
Good afternoon.
How using yii is it more elegant and architecturally more correct to implement hiding / displaying view elements for certain roles, and even taking into account business rules.
For example, we have 2 roles
private_user and common_user
The first role allows you to see the price column on the page. The second is not.
When displaying the pricing table in the view, I get this:
<?php foreach ($prices as $p): ?>
<tr class="data-row">
<td><?php echo $p['code']; ?></td>
<td><?php echo $p['name']; ?></td>
<td class="price"><?php echo Yii::app()->user->checkAccess('view_price') ? $p['price'] : " "; ?></td>
</tr>
<?php endforeach;?>
Answer the question
In order to leave comments, you need to log in
It is possible absolutely different . To do this in the controller:
if (Yii::app()->user->checkAcces('...')) {
$viewName = 'with_price_view';
}
else {
$viewName = 'without_price_view';
}
$this->render($viewName, ...);
$data = array(
'prices' => $prices,
'showPrice' => Yii::app()->user->checkAccess('...'),
);
$this->render('...', $data);
<?php foreach ($prices as $p): ?>
<tr class="data-row">
<td><?php echo $p['code']; ?></td>
<td><?php echo $p['name']; ?></td>
<?php if ($showPrice): ?>
<td class="price"><?php echo $p['price'] ?></td>
<?php endif; ?>
</tr>
<?php endforeach;?>
Perhaps check the role of the controller and substitute the desired view in render. Or if the differences in the views are small, you can break the view into blocks inside the main template, connect the necessary blocks (renderPartial)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question