Data to View |
Hellu,
![]() Let's say I want to make my controller a little bit more cleaner and my view more "attractive". At the moment I'm using this code in my controller method: PHP Code: [...] ($this->site.. is a reference to the model) The method createCategoryPageString, who I'm referring to, looks like this: PHP Code: private function createCategoryPageString($result) As you can see, both methods using string concatenation to create a string, that represents an article. The problem is: all this happens in the controller, which should only use logic and not build the html for the view, right? The $catString is then send to the view and I can output the result with echo, but I'm curious how I could write it in the view and make everything a little bit more clean. I know I could just write it down but then the logic is just in the view and argh.. can't get it. How should I do such things? Or is this the right way? -.-.-.-.-.-.-.-.-
![]()
For similar cases my habit is using HMVC. Your current code does not look well, indeed, redesign would be difficult.
In general, you can clean things up by only building an array of data in your controller, passing that array to your view, and then letting your view insert the HTML. You can put arrays within arrays in the data array you send your view, and in your view, it's perfectly acceptable to use loops and conditional logic (if-then-else).
My brain's a little soft right now, so pardon me for not following your code, but I think I know what you mean. Suppose you had an array of friends, but not everyone had an email address. To display those without an email address, you could do something like this: PHP Code: // in your controller But it would be better to do something like this: PHP Code: // in your controller That way, there is no HTML code in your controller. In my experience, the more complex the data, the more you gain by creating the data structure in the controller, and letting the view handle all the HTML. The PHP alternate syntax I used in the second example may seem verbose at first, but it results in view files that are easier to read and that generate cleaner HTML without having to include a bunch of "\n" and "\t" in your strings.
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Okay... thanks for reply.
![]() I tried to clean the whole mess up and this is the result: Controller-method: PHP Code: [...] the getArticle-method (before: getCategoryPageString()): PHP Code: private function getArticles($result) and finally a part of my view: PHP Code: [...] This is much better and the view is well-feed. Is this the way to go? (sry for asking so often, but I would like to get the whole view thing, because, at least I think, it's hard to seperate :/) Quote:The PHP alternate syntax I used in the second example may seem verbose at first, but it results in view files that are easier to read and that generate cleaner HTML without having to include a bunch of "\n" and "\t" in your strings.Hihi.. even if a lot of people don't like this syntax, I really do when working with mixed html/php files. ![]() -.-.-.-.-.-.-.-.-
![]()
Yes, what you did is much better. Good job.
The only thing is the religious war between <?= and <?php echo. I prefer the first, but Codeigniter coding rules/conventions require the second. Also, many Open Source projects won't accept pull requests using the first. Just be aware of it. ![]()
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Code: The only thing is the religious war between <?= and <?php echo. I prefer the first, but Codeigniter coding rules/conventions require the second. Also, many Open Source projects won't accept pull requests using the first. Just be aware of it. Smile Thanks for helping, I appreciate it. (: -.-.-.-.-.-.-.-.-
![]() (04-10-2015, 08:59 PM)RobertSF Wrote: The only thing is the religious war between <?= and <?php echo. I prefer the first, but Codeigniter coding rules/conventions require the second. Also, many Open Source projects won't accept pull requests using the first. Just be aware of it.Where does CI specify to use full PHP open tags? http://www.codeigniter.com/user_guide/ge...tive-echos There's even a setting in config to use short tags, which will convert to full open tags via regex if php.ini doesn't allow them. (04-11-2015, 10:01 AM)CroNiX Wrote: Where does CI specify to use full PHP open tags? Here: http://www.codeigniter.com/user_guide/ge...-open-tags So.. I think he just wanted to say, that when you use PHP < 5.4 it would be smarter to not use them. But that CI can convert the tags automatically is something I didn't know, maybe a thing to check out. -.-.-.-.-.-.-.-.-
![]()
The reason for the war was that years ago the short tags interfered with the xml opening tag.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
|
Welcome Guest, Not a member yet? Register Sign In |