tags:

views:

52

answers:

2

I am upgrading my site from some old spaghetti coding to some nice, clean custom MVC structure (and having fun learning in the process).

On my page to display blog listings, I had a function that would help my dynamically build HREF's for the a links - to keep track of applied filters via $_GET...hard to explain...but here it is:

/* APPLY BROWSE CONTROLS / FILTERS 
 | this function reads current $_GET values for controlling the feed filters,
 | and replaces the $value with the desired new $value
*/
function browse_controls($key,$value=null,$ret='url') {

 // find current control settings
 $browse_controls = array();
 if(array_key_exists('browse',$_GET)) { $browse_controls['browse'] = $_GET['browse']; }
 if(array_key_exists('display',$_GET)) { $browse_controls['display'] = $_GET['display']; }
 if(array_key_exists('q',$_GET)) { $browse_controls['q'] = $_GET['q']; }

 // replace desired setting
 if($value) {
  $browse_controls[$key] = $value;
 }else{
  unset($browse_controls[$key]);
 }

 // build url
 $url = ABS_DOMAIN . 'sale/';
 if(!empty($_GET['cat'])) { $url .= $_GET['cat'] . '/';}
 if(!empty($_GET['sub'])) { $url .= $_GET['sub'] . '/';}
 $url .= '?' . http_build_query($browse_controls);
 return $url;
}

I could call this query by simply:

<a href='<?php echo browse_controls('browse',$prev_page); ?>' class="crumb">Previous Page</a>

How can I achieve that same with MVC structure and full separation of presentation and logic. Are functions allowed in my template?

Help!

+1  A: 

I am not sure about your particular framework, but in Ruby on Rails and ASP.NET MVC, this kind of stuff is goes to the helper classes. They are UI concerns. So now logic here. Just formatting, transforiming, building HTML. You can place them near of the templates, or give them separate directory.

What I can reccomend is to inspect examples boundled with your framework. Often they give good knowlege of the framework.

Mike Chaliy
hmm thats cool, i haven't heard of helper classes before...does the presentation class call them? what calls them?
johnnietheblack
they are called from template
Mike Chaliy
+2  A: 

I use the following test: is the code I'm tempted to write in 'presentation space' deciding on what to present, or how to present it? If the answer is what, then it feels more like business logic and probably belongs outside of the template. If it's how to present it, then that belongs in the template (or in associated support functions).

For example, let's say you are offering a 10% discount for certain users. Computing specific prices before/after the discount is clearly business logic. Testing if there is a discount and then showing it in red is presentation logic.

Of course, there are always exceptions to the rules, but those should be acknowledged as such and you should specifically state in a comment why you are making the exception.

Peter Rowell
sweet good logic there...which do you think applies to dynamically building html for href? it somehow seems but what and how to me..haha
johnnietheblack
All my objects have a url() method that returns the canonical URL for that object. I have no problems with calling that from a template. But it returns *only* the URL, not a fully formatted A tag.
Peter Rowell