简体   繁体   English

在Codeigniter视图中有条件语句是否干净?

[英]Is it clean to have conditional statements within Codeigniter views?

I am currently trying to separate my Views as much as possible from my Controllers. 我目前正试图尽可能地将我的观点与我的控制器分开。 Ideally I would like to have minimal PHP in my view, except variable names, etc. These variables are being passed in from the controller. 理想情况下,我想在我的视图中使用最小的PHP,除了变量名等。这些变量是从控制器传入的。

But is it clean to have IF statements (or the like) within a view? 但在视图中使用IF语句(或类似内容)是否干净?

For example 例如

// Controller 

$data['status'] = 'pass';

$this->load->view("Status Page", $data);

And .. 并..

<!-- View -->
<div>
    <?php if($status === 'pass') { ?>
    <img src='passIcon.jpg'>Pass
    <?php } else { ?>
    <img src='failIcon.jpg'>Fail
    <?php } ?>
</div>

The closest thing I found to an answer on SO was Conditionals in Views 我在SO上找到的最接近的答案是视觉中的条件

This was for ASP, and I guess the principles still apply. 这是针对ASP的,我猜这些原则仍然适用。 I could bring the conditional statements back into the controller, but then the controller would be creating HTML and sending it to the view, which isn't right either. 我可以将条件语句带回控制器,但是然后控制器将创建HTML并将其发送到视图,这也是不对的。

Is there any way to avoid this cross over? 有没有办法避免这种交叉? Or will there always be fragments of PHP in views? 或者视图中是否总会有PHP片段?

From my point of view, it's the view's job to render the data , so if you need conditions to display it proprely then by all means do it , as it will avoid duplicate html code to split it in 2 views and test the var in the controller . 从我的角度来看,渲染数据是视图的工作,所以如果你需要条件来真实地显示它,那么一定要做,因为它会避免重复的html代码将它拆分为2个视图并测试var中的控制器。

Allso another good practice would be to use the alternative syntax in the view as it makes following gode much easyer . 另外一个好的做法是在视图中使用替代语法,因为它使跟随gode更容易。 Eg : 例如:

<!-- View -->
<div>
    <?php if ( $status === 'pass' ) : ?>
        <img src='passIcon.jpg'>Pass
    <?php else : ?>
        <img src='failIcon.jpg'>Fail
    <?php endif; ?>
</div>

However taking you're example a bit further you can have the src set in the controller ( i must admit that there will be times where you'll need conditional in views ) : 然而,再举几个例子你可以在控制器中设置src(我必须承认有时你需要在视图中使用条件):

Controller 调节器

$data['src'] = ( $data['status'] === 'pass' ) ? 'passIcon.jpg' : 'failIcon.jpg';
$data['text'] = ( $data['status'] === 'pass' ) ? 'Pass text' : 'Fail text';

$this->load->view("Status Page", $data);

View 视图

<!-- View -->
<div>
    <img src='<?php echo $src; ?>'><?php echo $text; ?>
</div>

Different frameworks have different accepted practices; 不同的框架有不同的接受做法; in Django, for example, it's frowned on to put any logic in template files. 例如,在Django中,不赞成将任何逻辑放在模板文件中。

I've always preferred to keep the controller relatively clutter-free though - especially if it's something as simple as choosing between two images to display. 我总是倾向于保持控制器相对无杂乱 - 特别是如果它像在两个图像之间进行选择一样简单。

Ultimately, it's your choice, but for me, simple conditionals and loops are fine in templates. 最终,这是你的选择,但对我来说,简单的条件和循环在模板中都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM