Best practice for placement of display logic in a view in CakePHP
-
02-07-2019 - |
Question
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?
Solution
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.
OTHER TIPS
Build your own AppHtmlHelper which extends HtmlHelper and does the check for you.