MVC:我应该在哪里格式化数据? [英] MVC: Where should I format data?

查看:109
本文介绍了MVC:我应该在哪里格式化数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Model(一个有数据的数组)获取数据,我需要以特定格式显示。我需要迭代数组,格式化数据,然后显示它。我应该在哪里格式化数据显示?在模型,控制器或视图?
谢谢。

I get data from Model (an array with data) and I need to display with a specific format. I need to iterate over the array, format the data and then display it. Where should I format data to display? In Model, Controller or View? Thank you.

推荐答案

在视图中对数组进行迭代并显示数据。因此我也会在视图中进行格式化。如果格式化很复杂和/或需要大量代码,请将其放入辅助函数中。

Iteration over an array and displaying the data is done in the view. Therefore I would also do the formatting in the view. If formatting is complicated and/or requires a lot of code, put it in a helper function.

例如:

检视:

<?php foreach($array as $item): ?>
    <p><?php echo format_function($item); ?></p>
<?php endforeach; ?>

助手:

function format_function($text)
{
    // Do some formatting here...
    return $formatted_text;
}

这篇关于MVC:我应该在哪里格式化数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆