redbullmarky Posted May 11, 2008 Share Posted May 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105114-constructing-complex-views/ Share on other sites More sharing options...
448191 Posted May 11, 2008 Share Posted May 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105114-constructing-complex-views/#findComment-538157 Share on other sites More sharing options...
redbullmarky Posted May 11, 2008 Author Share Posted May 11, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/105114-constructing-complex-views/#findComment-538168 Share on other sites More sharing options...
448191 Posted May 11, 2008 Share Posted May 11, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/105114-constructing-complex-views/#findComment-538173 Share on other sites More sharing options...
redbullmarky Posted May 11, 2008 Author Share Posted May 11, 2008 great stuff thanks. interested in any other approaches too, if yourself or anyone else has any Quote Link to comment https://forums.phpfreaks.com/topic/105114-constructing-complex-views/#findComment-538174 Share on other sites More sharing options...
448191 Posted May 11, 2008 Share Posted May 11, 2008 Typo: 'static property of the Registry' needs to be 'static property or the Registry'. Quote Link to comment https://forums.phpfreaks.com/topic/105114-constructing-complex-views/#findComment-538178 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.