Your First Template

There are two basic components to any Smarty page: the template file, and a PHP file. Let's first create our template file. It needs to be created in our templates folder.

An example templates/index.tpl file
// Test a template comment
{* Smarty *}

Hello world!  {$name} is now successfully running Smarty!

Take a moment to note the syntax used by Smarty. Smarty code is simply entered between curly bracket characters ({}). Later on you will see that template files are nothing more than an HTML page with Smarty syntax inside it.

Now let's move on to our PHP file that will use this template created in the previous step. Create this PHP file in your website's root.

An example index.php file
<?php
require_once('Smarty.class.php');

// Limited installation users ONLY:
// Uncomment the line below and remove the line above
// require_once(SMARTY_DIR . 'Smarty.class.php');

// Create our Smarty object
$smarty = new Smarty();

// Set the Smarty directories
$smarty->template_dir = '/wwwroot/example_site/templates/';
$smarty->compile_dir  = '/wwwroot/example_site/templates_c/';
$smarty->config_dir   = '/wwwroot/example_site/configs/';
$smarty->cache_dir    = '/wwwroot/example_site/cache/';

// Assign a value to a smarty variable
$smarty->assign('name', 'Bernard');

// Display our template
// We do not need to specify that it is in 'templates/'
// because we have assigned the Smarty directories
$smarty->display('index.tpl');
?>

You are now officially a Smarty user! You may have begun to see some of the possibilities of Smarty as we moved through these initial steps. Rest assure that this is only the beginning of what Smarty can do.

<- Previous Next ->

{/literal}