Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separating code from layout in template

I have an app built around an MVC pattern. The view is php but mostly html with minimal php code embedded, stuff like this -

Welcome <?php echo $USERNAME ?>

and

<table>
<?php foreach ($USERS as $row) : ?>
<tr><td><?php echo $row->name ?><td><?php echo $row->address ?></tr>
<?php endforeach ?>

I only want display layout logic in this file,and I want to keep it simple!

This is mostly working really weel for me however I'm struggling a bit certain aspects. For example look at the table in the code above and imagine that each column (name, and address) has a title NameAddress

Now imagine I want to make the columns sortable. So I make it something like -

<tr><th><a href="?sort=name">Name</a><td><a href="?sort=addr">

But this isn't good enough. My view needs to look at which column is being sorted by and add an up or down arrow. It needs tochange the link of the currently sorted column to ?sort=name_reverse if that column is already being sorted so that clicking on it sorts it the other way. It's a bit too complicated to write nice neat code in my template now...

So I can get my controller to create variables containing -

<tr><th><?php echo $HEADING[0] ?><th><?php $HEADING[1] ?> 

etc. BUT this means that my controller is now generating actual HTML which is really the responsibility of the page template. It removes the ability of the template to format page in some different ways... And just feels wrong.

But how can I best deal with this, where I feel I need my page controller to be generating variables containing HTML...

Any suggestions?

like image 862
jcoder Avatar asked Jan 12 '11 10:01

jcoder


3 Answers

Separate the code to create the table into a ViewHelper. Configure the method call from the template. Have the ViewHelper render the actual table with all required settings. Your main template should then contain only a line like this:

<?php echo table($data, $sortOptions, $otherConfig) ?>

Basically, any logic required to build that table will be contained within the ViewHelper which is why you might want to make it a class. The complete implementation will vary depending on your needs.

If you dont want to put all the logic into one large Table Helper, consider making helper functions for any parts of the table. For instance, something like

<th><?php echo sortLink('Name', $current); ?></th>

This way, the sortLink helper would decide whether and how to create and insert any sort links, so you dont have to have that logic or any conditionals in the main template.

like image 162
Gordon Avatar answered Nov 15 '22 13:11

Gordon


Excellent question, which is very uncommon for this site.

First of all you HAVE to accept the fact that any real life template will never be simple. That's it. It's called presentation logic and it follows the complexity of the presentation itself.

So, just make it the same way as before -- by using the same loop and conditional operator and -- possibly -- some home-brewed helper functions, which will spoil template purity but greatly helps.

In fact, you have almost nailed it. you can use predefined variables, however they should contain no HTML but just values to be displayed.

<tr>
 <th>
  <a href="<?=$links['name']['url']?>">
   Name
   <?if($links['name']['dir']=='asc'):?><img src="arrow.gif"><?endif?>
   <?if($links['name']['dir']=='desc'):?><img src="reverse_arrow.gif"><?endif?>
  </a>
 <td>

it is tempting to make a loop out of this but I would advise against that, because it will lead you to inventing some HTML builder which is another story and not that reliable as pure HTML itself.

The only blame for you: a programmer, asking for the code assistance, should bring the code, not wordy blab. An example of the actual HTML would be much better, allowing answerers not to invent it out of the head.

like image 22
Your Common Sense Avatar answered Nov 15 '22 13:11

Your Common Sense


If I may add a suggestion, you can convert all PHP output to DOM/XML and feed it to an XSLT processor where the presentation, however complex, is more consistent, because XSLT and XHTML are both based on XML. You do get presentation separated from content both in terms of physical location and technology used to deliver it.

like image 36
Dennis Kreminsky Avatar answered Nov 15 '22 13:11

Dennis Kreminsky