January 28th, 2013, 12:08 AM
Shrinking the view
Lates say I want to write some code that creates a drop down that shows months of the year.
I can easily write loose php in view which sucks:
Now in order to keep my views clean for genious desginers, would this be the right thing to do?
$m = 0;
?><option value="<?php echo $m;?>" <?php if($post_m ?> selected="selected" <?php } ?>
><?php echo date("F", mktime(0, 0, 0, $m));?></option><?php
And in libraries folder I ll have:
$data['months'] = $this->drop_down->index('month');
And in view
protected $posted_month = "";
public function index($name=NULL)
$CI =& get_instance();
$CI->posted_month = $CI->input->post($name) ? $CI->input->post($name) : "";
$m = 0;
$CI->dropdown.="<option value=\"".$m."\""; if($CI->posted_m
$CI->dropdown.= ">\n".date("F", mktime(0, 0, 0, $m))."</option>";
Please advice. Thanks
<?php echo $months;?>
January 28th, 2013, 09:06 PM
Don't cater specifically to designers. Either they're smart enough to understand PHP, or they're smart enough to understand what to touch and what not to touch, or they're not smart enough to be mucking around in your application's views.
Your views will be a bit clearer if you use the alternative PHP block syntax:
Alternatively, create a new view with that snippet of code and include it in your main view using load->view. Then tell your designers not to touch the partial view snippets.
<?php for($m = 1; $m <= 12; $m++): ?>
<option value="<?php echo $m;?>"
<?php if(??): ?> selected="selected" <?php endif; ?>>
<?php echo date("F", mktime(0, 0, 0, $m));?>
<?php endfor; ?>
January 29th, 2013, 01:27 AM
Another thing to remember when you're doing things like this:
Don't think about "clean" (as far as templating goes), think about "re-use". Your example is actually a very good one because there's a high chance that you'll want to have a months select elements in more then one place, so that's when you should start looking at it to extract that out into it's own helper or function, so I think that you've got the right idea. Depending on how you've got the rest of your system set out you can incorporate E-Oreo's suggestion of using that with a separate snippet for the HTML layout pretty easily.