在 CakePHP 视图中放置显示逻辑的最佳实践
我有一个 CakePHP 1.2 应用程序。
我遇到了需要执行一些逻辑才能在视图中正确渲染内容的情况。
例子: 我有一个名为“类型”的表。 有各种类型的标志,因此每一行都设置了一个或多个 BOOL 字段。 对于一种类型,我将 RTF 的内容存储在另一个字段中。
因此,当我在视图中显示所有拼写错误的列表时,我想为存储 RTF 的类型创建一个链接。 我不想显示链接,否则。
这个逻辑最好的地方在哪里?
我想使用 html->link 创建链接,但我无法在模型内部使用它。 逻辑是否应该放入模型中?如果是,我是否应该在模型中构建 HTML,例如链接?
I have a CakePHP 1.2 application.
I'm running into the case where I need to do some logic to render things correctly in the view.
Example:
I have a table called Types. There are flags for various types, so each row has one or more BOOL fields set. For one type of type, I store the contents of an RTF in another field.
So when I display a listing of all typoes in the view, I want to create a link for the types that are the type where I store RTF. I don't want to show a link, otherwise.
Where is the best place for this logic?
I want to use html->link to create the link, but I can't use that inside of my model. Should the logic go in the model, and if so, should I be building HTML in my model, e.g. the link?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议将该逻辑保留在视图中,在模型中构建 HTML 会破坏整个视图/模型的分离。 我不认为视图逻辑有什么问题,我个人倾向于将其限制为基本条件。
最终我认为这个想法是找出这个逻辑最适合的地方。 在这种情况下,逻辑是,“如果我有某种类型,我应该提供指向它的链接”,这是视图问题,而不是模型问题。
i'd suggest keeping that logic in the view, building HTML in your model breaks the whole view/model separation. i don't think there's anything wrong with view logic, personally i tend to constrain it to basic conditionals.
ultimately i think the idea is figuring out where this logic best fits. in this case, the logic is, "if i have a certain type, i should provide a link to it", which is a view problem, not a model problem.
构建您自己的 AppHtmlHelper,它扩展 HtmlHelper 并为您进行检查。
Build your own AppHtmlHelper which extends HtmlHelper and does the check for you.