Jump to content

Constructing complex Views


Recommended Posts

Hi All

Consider this page:

 

http://www.timesonline.co.uk/tol/sport/football/

 

I'm after a few thoughts on how some of you might actually build a dynamic page like this using an MVC-type approach.

From what I've tried, I either get too much presentational code inside my controllers, or too much code inside my templates. Now - whilst I can appreciate that it's cool to put code inside templates as long as it's display logic, it's still a little messy. A simple blog listing template might go something like:

 

<? foreach($articles as $article): ?>
   <div class="article">
      <div class="article_inner">
         <h2><?=$article['title'] ?></h2>
         <p><?=$article['teaser'] ?></p>
      </div>
   </div>
<? endforeach ?>

 

neat, but that just produces a single list in one column. What I'm looking for is a completely flexible layout where I can display single headlines, subheadlines, lists, related links ,etc.

 

The way I've thought is to seperate each "block" (ie, a list, group of subheadlines, a headline, etc) into seperate block templates, which are rendered by the controller and 'appended' to the main basic newsy layout in chunks. ie:

 

<?php
// code to render headline, subheads, etc here
..
..

// now build the page
$tpl->append('left', $headline); // headline is the rendered headline template
$tpl->append('left', $subheads); // subheads are 2/3 sub headlines displayed on single line
// etc

return $tpl->render();
?>

 

Then comes things like 'widgets' for stuff like 'most read', 'videos', etc. I'm guessing, to leave the control over whether they are/not displayed down to the designer, that these widgets would need to be rendered into individual variables for the designer to decide whether they show, rather than just appending them to mandatory content.

 

can anyone think of any other ways I could implement a complex layout whilst still keeping flexibility for the template designer? I'm not really looking for how Drupal, Django, et al do it - and I'm not wanting to use a tempate engine of sorts, regardless of how much cleaner they might make things - just thoughts that you lot might have.

 

Cheers

Mark

Link to comment
Share on other sites

You might take an example from ZF, which has the Action View Helper. In short, it allows you to place calls to controllers in your template, reducing repeating logic and isolating business logic associated with constructing a specific part of the view.

 

http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.action

Link to comment
Share on other sites

looks interesting. have come across the concept of view helpers before, but not really in terms of introducing entire blocks of stuff in. this might sound picky, but with Zend's way of doing things - would I be right in thinking that if I had a "widget" called in one of my templates such as:

 

<?php echo $this->action('test', 'mycontroller'); ?>

 

that I could invoke the same via the URL, such as: mysite.com/mycontroller/test ? couldnt that proove not so good? (I only ask this last question as I'm actually tempted to jump into ZF properly and give it a good go)

Link to comment
Share on other sites

Yes you could.

 

If you really want to avoid that (because for example you don't want to allow linking to a partial view, you could, for example, use a simple request token.

 

In short, you create a unique token for each request that you can pass to the controller to ensure that the controller is called within the same request. You can also use a complex object as a request token. You could store the initial dispatching token (using a front controller plugin) in a static property or the Registry. If a 'widget controller' finds that the initial token has not been dispatched (you can just check isDispatched()), it could fail.

 

Either method would work.

 

 

 

 

edit: corrected typo as detailed below :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.