N
N
name_a2013-12-17 11:15:03
Yii
name_a, 2013-12-17 11:15:03

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'] : "&nbsp;"; ?></td>
  </tr>
<?php endforeach;?>

Is it possible to slip different views for different roles? But how to implement it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vit, 2013-12-17
@name_a

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, ...);

And it is possible with one view, but without unnecessary business logic inside the template. In the controller like this:
$data = array(
   'prices' => $prices,
   'showPrice' => Yii::app()->user->checkAccess('...'),

);
$this->render('...', $data);

And in the template like this:
<?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;?>

T
TekVanDo, 2013-12-17
@TekVanDo

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)

T
Timur Tuz, 2013-12-17
@TTA

If the logic is completely different, then it makes sense to make 2 controllers for guests and authorized ones. Specify different layouts and logic in them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question