Using Smarty
To use Smarty, you just need to make it available to the executing script, typically by
way of the require() statement:
require("Smarty.class.php");
With that complete, you can then instantiate the Smarty class:
$smarty = new Smarty;
That??™s all you need to do to begin taking advantage of its features. Let??™s begin with
a simple example. Listing 19-3 presents a simple design template. Note that there are
two variables found in the template: $title and $name. Both are enclosed within curly
brackets, which are Smarty??™s default delimiters. These delimiters are a sign to Smarty
that it should do something with the enclosed contents. In the case of this example,
the only action is to replace the variables with the appropriate values passed in via the
application logic (presented in Listing 19-4). However, as you??™ll soon learn, Smarty is
also capable of doing a variety of other tasks, such as executing presentational logic
and modifying the text format.
478 CHAPTER 19 ?– T EMPLAT I NG WITH SMARTY
Listing 19-3. A Simple Smarty Design Template (templates/welcome.tpl)
{$title}
Hi, {$name}. Welcome to the wonderful world of Smarty.
Also note that Smarty expects this template to reside in the templates directory,
unless otherwise noted by a change to $template_dir.
Listing 19-4 offers the corresponding application logic, which passes the appropriate
variable values into the Smarty template.
Pages:
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560