First, it loads config.php, which sets some global variables,
and then it loads the Smarty template file, which will generate the actual HTML content when a client requests
index.php.
The standard way to create and configure a Smarty page is shown in the following code snippet:
// Load the Smarty library
require_once SMARTY_DIR . 'Smarty.class.php';
// Create a new instance of the Smarty class
$smarty = new Smarty();
$smarty->template_dir = TEMPLATE_DIR;
$smarty->compile_dir = COMPILE_DIR;
$smarty->config_dir = CONFIG_DIR;
?>
In TShirtShop, we created a class named Application that inherits from Smarty, which contains the initialization
procedure in its constructor. This makes working with Smarty templates easier. Here??™s again the code of the
Application class:
/* Class that extends Smarty, used to process and display Smarty
files */
class Application extends Smarty
CHAPTER 3 ?– STARTING THE TSHIRTSHOP PROJECT 48
{
// Class constructor
public function __construct()
{
// Call Smarty's constructor
parent::Smarty();
// Change the default template directories
$this->template_dir = TEMPLATE_DIR;
$this->compile_dir = COMPILE_DIR;
$this->config_dir = CONFIG_DIR;
}
}
?– Note The notion of constructor is specific to object-oriented programming terminology. The constructor of
a class is a special method that executes automatically when an instance of that class is created.
Pages:
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117