What are the best practices for using a template engine like Smarty in PHP to separate code logic from presentation?
When using a template engine like Smarty in PHP, it is important to separate code logic from presentation to improve code readability and maintainability. To achieve this, you should avoid placing complex logic within the template files and instead move it to the PHP files that interact with the template engine. This way, the template files only contain presentation-related code, making them easier to manage and update.
<?php
// PHP file interacting with Smarty template engine
require_once('smarty/libs/Smarty.class.php');
$smarty = new Smarty;
// Assign variables to be used in the template
$smarty->assign('title', 'Welcome to my website');
$smarty->assign('content', 'This is the content of the page');
// Display the template file
$smarty->display('index.tpl');
?>