Smarty template logic

1

Our development team had a bit of a disagreement and discussion today on where logic should go when using Smarty.  My personal belief was that when using the MVC design pattern like we do, logic should go in the controller and not in the view layer (smarty template).

The existing code was as follows:

1
2
<!-- controller -->
$smarty->assign('pageAction', $_GET['pageAction']);
1
2
3
4
5
6
7
8
9
<!-- smarty template (view) -->
<h2>
DNS Domains 
{if $pageAction == 'viewDisabled'}
	Viewing Disabled
{elseif $pageAction == 'viewPendingActions'}
	Pending Actions
{/if}
</h2>

My belief was to move the logic to the controller and out of the view layer. The change was as follows:

1
2
3
4
5
6
7
8
9
10
11
12
 
// set the page title in the controller and assign variable to smarty
switch($pageAction) {
	case 'viewDisabled':
		$smarty->assign('pageTitle', 'Disabled DNS Domain List');
		break;
	case 'viewPendingActions':
		$smarty->assign('pageTitle', 'Pending Action DNS Domain List');
		break;
	default:
		$smarty->assign('pageTitle', 'DNS Domain List');
}
1
2
<!-- smarty template -->
<h2>{$pageTitle}</h2>

Both methods produce the same result. Is either of them more correct than the other?

Posted in: Smarty
Tags: ,

This article has 1 comment

  1. Hung Bui 11/05/2009, 7:46 am:

    I prefer the second option. We really should keep all programming logic in PHP separated from html template. This would definitely help web designers who only know about xhtml and css still can do his jobs without worrying about programming “logic”!

Leave a Reply

Your email address will not be published. Required fields are marked *

*




You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">