Basics
Installation
For installation instructies, please see the getting started section.
Rendering a template
Here's how you create an instance of Smarty in your PHP scripts:
You now have a Smarty object that you can use to render templates.
<?php
require 'vendor/autoload.php';
use Smarty\Smarty;
$smarty = new Smarty();
$smarty->display('string:The current smarty version is: {$smarty.version}.');
// or
echo $smarty->fetch('string:The current smarty version is: {$smarty.version}.');
Using file-based templates
You probably want to manage your templates as files. Create a subdirectory called 'templates' and then configure Smarty to use that:
<?php
require 'vendor/autoload.php';
use Smarty\Smarty;
$smarty = new Smarty();
$smarty->setTemplateDir(__DIR__ . '/templates');
Say you have a template file called 'version.tpl', stored in the 'templates' directory like this:
You can now render this, using:
<?php
require 'vendor/autoload.php';
use Smarty\Smarty;
$smarty = new Smarty();
$smarty->setTemplateDir(__DIR__ . '/templates');
$smarty->display('version.tpl');
Assigning variables
Templates start to become really useful once you add variables to the mix.
Create a template called 'footer.tpl' in the 'templates' directory like this:
Now assign a value to the 'companyName' variable and render your template like this:
<?php
require 'vendor/autoload.php';
use Smarty\Smarty;
$smarty = new Smarty();
$smarty->setTemplateDir(__DIR__ . '/templates');
$smarty->assign('companyName', 'AC & ME Corp.');
$smarty->display('footer.tpl');
Run this, and you will see:
Note how the escape modifier
translated the &
character into the proper HTML syntax &
.
Read more about auto-escaping in the next section.