Welcome Guest, Not a member yet? Register   Sign In
How do you prevent Views from becoming an unreadable mess?
#1

[eluser]BrianDHall[/eluser]
I'm starting to run into this problem on my latest project that has lots of dynamic information in the view. Visual example of one view:

Code:
<h1 class="side">
    <a href="[removed]void(0)">db_image_details->id?&gt;&lt;?php echo $li->db_image_details->extension?&gt;','Window1','menubar=no,width=&lt;?php echo $li->db_image_details->width + 10?&gt;,height=&lt;?php echo $li->db_image_details->height + 10?&gt;,toolbar=no')">
        <img >db_image_details->id?&gt;-100-100.&lt;?php echo $li->db_image_details->extension?&gt;" />
    </a>
&lt;?php if ($li->premium)
{
    echo '<a href="' . base_url() . 'view_listing/' . ">seo_address()}/{$li->id}" . '">';
}
?&gt;
&lt;?php echo $li->address ?&gt; -
&lt;?php echo $li->city ?&gt;,
&lt;?php echo $li->state ?&gt;
&lt;?php if ($li->premium)
{
    echo '</a>';
}
?&gt;</h1>
<h3 class="side">&lt;?php echo $li->bedrooms ?&gt; bedroom, &lt;?php echo $li->bathrooms ?&gt; bath, &lt;?php echo $li->sqft ?&gt; sqft</h3>
<p>&lt;?php echo $li->propertytype->displayname ?&gt; with &lt;?php echo $li->garagetype->displayname ?&gt;<br>
    <br>
&lt;?php echo $li->briefdescription ?&gt;</p>
<div class="clear"></div>
<h3 class="side"><a >user->email ?&gt;">Email Landlord</a> - Phone: &lt;?php echo $li->user->phonenumber ?&gt;&lt;?php if ($li->user->brokeragename) echo ' - Listed by ' . $li->user->brokeragename; ?&gt;</h3>

<hr class="clear-contentunit" />

Now I know part of this is my fault and it could be done a bit more cleanly, especially in the h1 tag, and I actually use short tags and just replaced them here in hope of helping the forums highlighter (FAIL), but you get the idea.

Its just a mess, and this is just a small little view partial. This certainly isn't even really openable in an offline HTML editor, and its hard to edit it even in my IDE.

So what do you guys and gals do? I'm presuming this is where template engines come in, but I just want to make my views more easily readable, easier to edit, and maybe even openable in something like *gasp* Dreamweaver.
#2

[eluser]Colin Williams[/eluser]
It looks pretty tame to me. You probably could do a fair amount of concatenation in helpers or in the controller, or even in sub views, but I don't think you've gone too far.
#3

[eluser]jedd[/eluser]
[quote author="BrianDHall" date="1259628143"]
So what do you guys and gals do? I'm presuming this is where template engines come in, but I just want to make my views more easily readable, easier to edit, and maybe even openable in something like *gasp* Dreamweaver.[/quote]
[/quote]

Ha .. what I do would actually make your code less sociable with HTML editors, unfortunately.

When I see code like this:
Code:
&lt;?php echo $li->address ?&gt; -
&lt;?php echo $li->city ?&gt;,
&lt;?php echo $li->state ?&gt;
&lt;?php if ($li->premium)
{
    echo '</a>';
}
?&gt;

I just want to change it to:
Code:
&lt;?php
    echo $li->address ." - ". $li->city .", ". $li->state ;
    if ($li->premium)
        echo '</a>';
?&gt;

But I'm not limited by the need to have my views modifiable by someone unfamiliar with PHP (or rather, familiar only with HTML).

I suspect you really do want to look at a templating system if your goal is to make your PHP look less like PHP.
#4

[eluser]John_Betong[/eluser]
Hi Brian,

I first started in CodeIgniter December 2006 and have changed coding style umpteen times.

Now I endeavour to alwasy simplify the code. Vague memories about "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live"

Here is a sample of my latest codingt style:
[code]
<div id='box_left'>
&lt;?php echo $left ?&gt;
</div>

<div id='box_center'>
<div id='box_joke'>
...
...
...


<div id='advert_top'>
&lt;?php j_rnd(TRUE, '#ccc') /* require('_snazzy_top.php') */ ?&gt;

&lt;?php /* < ! -- google_ad_section_start -- > */ ?&gt;
<h2 style='font-size:16px; margin:0 0 1em 0; color:#00f'>
&lt;?php echo $joke_title ?&gt;
</h2>

&lt;?php
require('_search_results_box.php'); /* MAYBE NOT DISPLAYED */

// BEWARE - empty $joke_memo when there is a list
if ((isset($joke_memo)) AND (is_string($joke_memo)) AND strlen($joke_memo))
{
if (TRUE)
{
echo nl2br($joke_memo);
}else{
echo $this->typography->auto_typography($joke_memo);
}
echo isset($joke_shout) ? $joke_shout : '';
}

// works for lists, Searches and top_20
if (isset($thelot) AND is_array($thelot) AND count($thelot))
{
echo "<div id='thelot'>";
foreach($thelot as $row):
// echo alternator('string one', 'string two');
echo $row;
endforeach;
echo '</div>';
echo br(1) .$this->pagination->create_links();
}

// SPECIAL FOR PICTURES
if (isset($thepix) AND is_array($thepix) AND count($thepix)>0)
{
$i2=0;
foreach($thepix as $row):
if (($i2 % 3) ==0)
{
echo "<br class='clb' style='line-height:1px' />";
}
echo "<span style='float:left;width:30%;height:200px;margin:1em auto;text-align:center;borderConfusedolid 1px #eee'>";
echo $row;
echo '</span>';
$i2++;
endforeach;
echo "<br class='clb' />";
echo $this->pagination->create_links();
}

?&gt;
&lt;?php /* < ! -- google_ad_section_end -- > */ ?&gt;


&lt;?php /* < ! -- google_ad_section_start(weight=ignore) -- > */ ?&gt;

<div style='width:80%; margin:4.2em auto; color:#00a; font-size:1.2em'>
<b>Have you heard the one about:</b>
&lt;?php
#5

[eluser]t'mo[/eluser]
Factor the nastiest bits out into a library. For example, the second line that's generating the HREF could be pulled out. There are a couple advantages of this: 1) it cleans up the View code, and 2) if done correctly, can leave your codebase with smaller, more coherent and reusable units of code. You could even write unit tests for the library code.
#6

[eluser]Jondolar[/eluser]
If your goal is to separate application logic (or business logic) from your view, then you have succeeded. Your view will always have "view logic" such as "only display this if there are records". I know of three choices:

1. Do it just like you did
2. Use template tags to define your loops and your data (won't break as many html editors)
3. Write your own template system or use a 3rd party template system (not smarty).

I wrote my own template system because I have specific requirements that I could not find in any other system. However, I'd prefer to use the CI template tags as it is easy and keeps the code tidy.
#7

[eluser]BrianDHall[/eluser]
Thanks for everyone's advice. I've rather gone with Jondolar's #3 = use a 3rd part template system that isn't smarty Wink

To avoid thread duplication: http://ellislab.com/forums/viewthread/138068/




Theme © iAndrew 2016 - Forum software by © MyBB