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?


Leave a comment

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

One thought on “Smarty template logic

  • Hung Bui

    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”!