PHP MVC 视图循环

发布于 2024-08-18 07:21:57 字数 460 浏览 10 评论 0原文

我有一个“博客预览”模板 - 基本上只是所述博客的缩略图、标题和简短摘录,采用简洁的结构,旨在在列表中重复。

如上所述,我打算从模型中的数据库中提取网站上排名前 10 的博客,将它们传输到控制器,控制器将提供它们作为视图。在视图中,我需要循环遍历结果并为每个博客填充一个新的“博客预览”。

我当前的解决方案(我认为这可能会违反 MVC 的规则)是在视图模板中执行此操作:

foreach($this->blogs as $blog) {
    $tpl = new Output_Html();
    $tpl->title = $blog['title'];
    // ...assign other vars

    $tpl->render();
}

不知何故,这感觉像是不应该允许视图执行的操作?但是,我还能如何循环浏览主页模板内的“预览”模板?

帮助?

I have a template for a "blog preview" - which is basically just a thumbnail, title, and short excerpt of said blog in a nice concise structure built for repetition in a list.

As hinted above, I intend to pull the top 10 blogs on my site from the DB in my model, transfer them to the controller, which will supply them as for the view. In the view, I will need to loop through the results and populate a new "blog preview" for each blog.

My current solution (which I think may break the rules of MVC) is to do this in the view template:

foreach($this->blogs as $blog) {
    $tpl = new Output_Html();
    $tpl->title = $blog['title'];
    // ...assign other vars

    $tpl->render();
}

Somehow this feels like something the view shouldnt be allowed to do? But, how else would I be able to loop through the "preview" templates inside of the main page template?

Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清秋悲枫 2024-08-25 07:21:57

考虑到视图负责生成输出,您在这里所做的事情似乎没问题:您没有在视图中执行任何“计算/业务/类似的事情”。

我唯一的问题是你在这里写了很多代码;我宁愿将 $blog 数组/对象传递给整个视图,并让视图处理它 - 而不是分配 $blog 的每个属性到视图。

即这样的东西看起来(只是一个想法 - 由您决定如何适合您的 View 类) 更漂亮:

foreach($this->blogs as $blog) {
    $tpl = new Output_Html();
    $tpl->blog = $blog;
    $tpl->render();
}

这意味着,如果您的 blog 对象一旦发生变化,您只有一个视图可以编辑(添加或删除内容),并且您不必修改对该视图的每个调用来添加/删除 的一个组件/属性$博客

Considering the View is responsible for the generation of the output, what you are doing here seems OK : you are not doing any "calculation / business thing / anything like that" in your View.

The only problem I have is that you are writing a lot of code here ; I would rather like to pass the $blog array/object to the View a whole, and let the View deal with it -- instead of assigning each property of the $blog to the View.

i.e. something like this seems (just an idea -- up to you to see how this can fit with your View class) more pretty :

foreach($this->blogs as $blog) {
    $tpl = new Output_Html();
    $tpl->blog = $blog;
    $tpl->render();
}

This means that, if your blog object ever changes, you only have one View to edit (to add or remove stuff), and you don't have to modify each call to that view to add/remove one component/property of $blog.

自此以后,行同陌路 2024-08-25 07:21:57

控制器的职责是处理输入并将模型设置为某种状态。您的视图的责任是渲染模型。所以,我想说,这样做是可以的。这就像渲染部分视图或使用 ViewHelper。 View Helper 封装了动态业务数据的表示逻辑。

The controller's reponsibility is to handle input and to set the Model into a certain state. Your View's reponsibility is to render the Model. So, I'd say, this is okay to do. It's like rendering a Partial View or using a ViewHelper. View Helper encapsulate presentation logic for dynamic business data.

╰ゝ天使的微笑 2024-08-25 07:21:57

我同意帕斯卡的观点。无需在控制器上执行该操作,只需将其传递给视图并在那里执行其余操作即可。让控制器接收输入,通过模型对其进行预处理,然后为视图准备一切。这样,您仍然可以确保代码的可维护性。

I agree with Pascal. Instead of performing that operation on your controller, simply pass it to the View and do the rest of stuffs there. Let the Controller take in the inputs, preprocess it via Models, then prepare everything for the View. That way, you still ensure the maintainability of your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文