Views with lots of ifs/elses: getting messy |
[eluser]codex[/eluser]
The idea behind MVC is maintainability, dividing scripts into clearly structured parts of code. Pretty good idea in theory, but because I have a lot of if/else and foreaches in my views they tend to look very messy, which can't be good. So I'm guessing I'm doing something wrong. Sort of. I was just wondering how do you guys do it. Do you take care of the if/else in the controller and push the output to the view? Or are your views also messy?
[eluser]John_Betong[/eluser]
Hi Codex, What you want in your code is hind-sight! The way I have opted to use/abuse CodeIgniter is: 1. the page consists of "boxes of data" and each box/div is a separate entity. 2. endeavour to have absolutely no HTML formatting in the view 3. have a separate CSS file containing the look & feel. 4. yes my controller, libraries, models and helpers are messy View Code: <?php echo $header ?> Cheers, John_Betong
[eluser]codex[/eluser]
[quote author="John_Betong" date="1193682777"]Hi Codex, What you want in your code is hind-sight! The way I have opted to use/abuse CodeIgniter is: 1. the page consists of "boxes of data" and each box/div is a separate entity. 2. endeavour to have absolutely no HTML formatting in the view 3. have a separate CSS file containing the look & feel. 4. yes my controller, libraries, models and helpers are messy View Code: <?php echo $header ?> Cheers, John_Betong [/quote] Hey John, if all your views look like this I can imagine that the rest IS messy ;-) But what if you need to loop something like a <ul>?
[eluser]John_Betong[/eluser]
Hi codex, Quote: But what if you need to loop something like a <ul>? http://johns-jokes.com/joke/five_col http://ellislab.com/forums/viewthread/63078/ Check the two links above. The first link was an example for the thread on how to use PHP mod %3. Notice the replies since there always seems to be an easier way. The expression "standing on the shoulders of giants" springs to mind Cheers, John_Betong
[eluser]Escee[/eluser]
I got allot of if-elses for checking the array count, looping the array or show a message that there aren't any messages or something. Therefore Smarty has the {foreach else}, after coding a feature like that in the parser class it goes allot easier: Code: {if items} And you can still use the 'old' way: Code: {items} The parser class comes in handy at the time :-) Stefan
[eluser]John_Betong[/eluser]
Hi Escee, This is how I achieve the if/else statements using PHP. Code: // controller I am currently working on a Smarty site, 35,923Kb of zipped source code! The things one has to do to earn some money Cheers, John_Betong |
Welcome Guest, Not a member yet? Register Sign In |