Smarty Library Tutorial: first example for beginner


Smarty is a template engine for PHP. It help you to separate application logic and content from its presentation.

In this article you will experience a small example application.

First of all you need to download and install the library smarty from the official page of the project.

Create a file named index.php and write in this code:

 

include('Smarty.class.php');

// create new smarty object

$smarty = new Smarty;

// assign content

$smarty->assign('title', 'This is my article');

$smarty->assign('content', 'Hello word!');

// display the template

$smarty->display('template_index.tpl');

 

Create a file named template_index.tpl and write in this code:

 

<html>
<head>
<title>Info</title>
</head>
<body>
Title: {$title}
Content: {$content}
</body>
</html>

 

Finally, visit the index.php page...

Related articles