Welcome Guest, Not a member yet? Register   Sign In
Views with lots of ifs/elses: getting messy
#1

[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?
#2

[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 Sad

View
Code:
<?php echo $header ?>

<body id='<?=$PAGE ?>' >
    
  <div id='container'>
    <div id='box_title'>
        &lt;?php echo $title ?&gt;
    </div>
    
    <div id='inner'>
      <br/>
      <div id='box_left'>
        &lt;?php echo $left ?&gt;
      </div>

      <div id='box_center'>
        <div class='btn' style='margin:-0.6em auto 1em'>  
            &lt;?php echo $search ?&gt;
        </div>
        
        &lt;?php echo j_rndtop()?&gt;
        <h3 class='title_quote title_orange'>&lt;!-- bmp bgb bs1 --&gt;
          &lt;?php echo  $joketitle ?&gt;
        </h3>
  
        <div id='box_joke'>
          &lt;!-- google_ad_section_start --&gt;
            &lt;?php echo $joke ?&gt;
          &lt;!-- google_ad_section_end --&gt;
        </div>
      </div>&lt;?php /* box_center */ ?&gt;

      <div id='box_right' class='z99'>
          &lt;?php echo  $right ?&gt;&lt;!-- default to Google placing --&gt;
      </div>

      <div id='footer'>
        &lt;?php echo  $footer ?&gt;
      </div>  
        
      <div id='rndbgt'>
        &lt;?php echo  j_rndbgt(FALSE) ?&gt;
      </div>  

</div>
<br />
  </div>&lt;?php /* container */ ?&gt;


&lt;/body&gt;
&lt;/html&gt;
&nbsp;
Cheers,

John_Betong
&nbsp;
#3

[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 Sad

View
Code:
&lt;?php echo $header ?&gt;

&lt;body id='&lt;?=$PAGE ?&gt;' &gt;
    
  <div id='container'>
    <div id='box_title'>
        &lt;?php echo $title ?&gt;
    </div>
    
    <div id='inner'>
      <br/>
      <div id='box_left'>
        &lt;?php echo $left ?&gt;
      </div>

      <div id='box_center'>
        <div class='btn' style='margin:-0.6em auto 1em'>  
            &lt;?php echo $search ?&gt;
        </div>
        
        &lt;?php echo j_rndtop()?&gt;
        <h3 class='title_quote title_orange'>&lt;!-- bmp bgb bs1 --&gt;
          &lt;?php echo  $joketitle ?&gt;
        </h3>
  
        <div id='box_joke'>
          &lt;!-- google_ad_section_start --&gt;
            &lt;?php echo $joke ?&gt;
          &lt;!-- google_ad_section_end --&gt;
        </div>
      </div>&lt;?php /* box_center */ ?&gt;

      <div id='box_right' class='z99'>
          &lt;?php echo  $right ?&gt;&lt;!-- default to Google placing --&gt;
      </div>

      <div id='footer'>
        &lt;?php echo  $footer ?&gt;
      </div>  
        
      <div id='rndbgt'>
        &lt;?php echo  j_rndbgt(FALSE) ?&gt;
      </div>  

</div>
<br />
  </div>&lt;?php /* container */ ?&gt;


&lt;/body&gt;
&lt;/html&gt;
&nbsp;
Cheers,

John_Betong
&nbsp;[/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>?
#4

[eluser]John_Betong[/eluser]
Hi codex,

Quote: But what if you need to loop something like a <ul>?
&nbsp;
http://johns-jokes.com/joke/five_col
&nbsp;
http://ellislab.com/forums/viewthread/63078/
&nbsp;
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 Smile

Cheers,

John_Betong
&nbsp;
#5

[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}
- {item}
{else}
- <i>No items found</i>
{/items}

And you can still use the 'old' way:

Code:
{items}
- {item}
{/items}

The parser class comes in handy at the time :-)

Stefan
#6

[eluser]John_Betong[/eluser]
Hi Escee,

This is how I achieve the if/else statements using PHP.

Code:
// controller
    $data['debug']  = unix_to_human(time());
    $data['header'] = $this->load->view('_header', $data, TRUE);


// view - header.ph[
  &lt;title&gt;&lt;?php echo isset($debug) ? $debug : $htmltitle ?&gt;&lt;/title&gt;
&nbsp;
I am currently working on a Smarty site, 35,923Kb of zipped source code! The things one has to do to earn some money Sad
&nbsp;
Cheers,

John_Betong
&nbsp;




Theme © iAndrew 2016 - Forum software by © MyBB