| 148 |
lars |
1 |
What is Smarty?
|
|
|
2 |
==============
|
|
|
3 |
|
|
|
4 |
## Requirements
|
|
|
5 |
Smarty can be run with PHP 7.1 to PHP 8.2.
|
|
|
6 |
|
|
|
7 |
## Installation
|
|
|
8 |
Smarty versions 3.1.11 or later can be installed with [Composer](https://getcomposer.org/).
|
|
|
9 |
|
|
|
10 |
To get the latest stable version of Smarty use:
|
|
|
11 |
```bash
|
|
|
12 |
composer require smarty/smarty
|
|
|
13 |
````
|
|
|
14 |
|
|
|
15 |
To get the latest, unreleased version, use:
|
|
|
16 |
```bash
|
|
|
17 |
composer require smarty/smarty:dev-master
|
|
|
18 |
````
|
|
|
19 |
|
|
|
20 |
To get the previous stable version of Smarty, Smarty 3, use:
|
|
|
21 |
```bash
|
|
|
22 |
composer require smarty/smarty:^3
|
|
|
23 |
````
|
|
|
24 |
|
|
|
25 |
Here's how you create an instance of Smarty in your PHP scripts:
|
|
|
26 |
```php
|
|
|
27 |
<?php
|
|
|
28 |
|
|
|
29 |
require 'vendor/autoload.php';
|
|
|
30 |
$smarty = new Smarty();
|
|
|
31 |
```
|
|
|
32 |
|
|
|
33 |
Now that the library files are in place, it's time to setup the Smarty
|
|
|
34 |
directories for your application.
|
|
|
35 |
|
|
|
36 |
Smarty requires four directories which are by default named
|
|
|
37 |
[`templates`](./programmers/api-variables/variable-template-dir.md),
|
|
|
38 |
[`configs`](./programmers/api-variables/variable-config-dir.md),
|
|
|
39 |
[`templates_c`](./programmers/api-variables/variable-compile-dir.md)
|
|
|
40 |
and
|
|
|
41 |
[`cache`](./programmers/api-variables/variable-cache-dir.md)
|
|
|
42 |
relative to the current working directory.
|
|
|
43 |
|
|
|
44 |
The defaults can be changed as follows:
|
|
|
45 |
```php
|
|
|
46 |
$smarty = new Smarty();
|
|
|
47 |
$smarty->setTemplateDir('/some/template/dir');
|
|
|
48 |
$smarty->setConfigDir('/some/config/dir');
|
|
|
49 |
$smarty->setCompileDir('/some/compile/dir');
|
|
|
50 |
$smarty->setCacheDir('/some/cache/dir');
|
|
|
51 |
```
|
|
|
52 |
|
|
|
53 |
The compile dir and cache dir need to be writable for the user running the PHP script.
|
|
|
54 |
|
|
|
55 |
> **Note**
|
|
|
56 |
>
|
|
|
57 |
> This is usually user "nobody" and group "nobody". For OS X users, the
|
|
|
58 |
> default is user "www" and group "www". If you are using Apache, you
|
|
|
59 |
> can look in your `httpd.conf` file to see what user and group are
|
|
|
60 |
> being used.
|
|
|
61 |
|
|
|
62 |
```bash
|
|
|
63 |
chown nobody:nobody /web/www.example.com/guestbook/templates_c/
|
|
|
64 |
chmod 770 /web/www.example.com/guestbook/templates_c/
|
|
|
65 |
|
|
|
66 |
chown nobody:nobody /web/www.example.com/guestbook/cache/
|
|
|
67 |
chmod 770 /web/www.example.com/guestbook/cache/
|
|
|
68 |
```
|
|
|
69 |
|
|
|
70 |
You can verify if your system has the correct access rights for
|
|
|
71 |
these directories with [`testInstall()`](./programmers/api-functions/api-test-install.md):
|
|
|
72 |
|
|
|
73 |
```php
|
|
|
74 |
$smarty = new Smarty();
|
|
|
75 |
$smarty->setTemplateDir('/some/template/dir');
|
|
|
76 |
$smarty->setConfigDir('/some/config/dir');
|
|
|
77 |
$smarty->setCompileDir('/some/compile/dir');
|
|
|
78 |
$smarty->setCacheDir('/some/cache/dir');
|
|
|
79 |
$smarty->testInstall();
|
|
|
80 |
```
|
|
|
81 |
|
|
|
82 |
Now, let's create the `index.tpl` file that Smarty will display. This
|
|
|
83 |
needs to be located in the [`$template_dir`](./programmers/api-variables/variable-template-dir.md).
|
|
|
84 |
|
|
|
85 |
```html
|
|
|
86 |
{* Smarty *}
|
|
|
87 |
Hello {$name}, welcome to Smarty!
|
|
|
88 |
```
|
|
|
89 |
|
|
|
90 |
> **Note**
|
|
|
91 |
>
|
|
|
92 |
> `{* Smarty *}` is a template [comment](./designers/language-basic-syntax/language-syntax-comments.md). It
|
|
|
93 |
> is not required, but it is good practice to start all your template
|
|
|
94 |
> files with this comment. It makes the file easy to recognize
|
|
|
95 |
> regardless of the file extension. For example, text editors could
|
|
|
96 |
> recognize the file and turn on special syntax highlighting.
|
|
|
97 |
|
|
|
98 |
Now lets edit our php file. We'll create an instance of Smarty,
|
|
|
99 |
[`assign()`](./programmers/api-functions/api-assign.md) a template variable and
|
|
|
100 |
[`display()`](./programmers/api-functions/api-display.md) the `index.tpl` file.
|
|
|
101 |
|
|
|
102 |
```php
|
|
|
103 |
<?php
|
|
|
104 |
|
|
|
105 |
require 'vendor/autoload.php';
|
|
|
106 |
|
|
|
107 |
$smarty = new Smarty();
|
|
|
108 |
|
|
|
109 |
$smarty->setTemplateDir('/web/www.example.com/guestbook/templates/');
|
|
|
110 |
$smarty->setCompileDir('/web/www.example.com/guestbook/templates_c/');
|
|
|
111 |
$smarty->setConfigDir('/web/www.example.com/guestbook/configs/');
|
|
|
112 |
$smarty->setCacheDir('/web/www.example.com/guestbook/cache/');
|
|
|
113 |
|
|
|
114 |
$smarty->assign('name', 'Ned');
|
|
|
115 |
$smarty->display('index.tpl');
|
|
|
116 |
|
|
|
117 |
```
|
|
|
118 |
|
|
|
119 |
> **Note**
|
|
|
120 |
>
|
|
|
121 |
> In our example, we are setting absolute paths to all of the Smarty
|
|
|
122 |
> directories. If `/web/www.example.com/guestbook/` is within your PHP
|
|
|
123 |
> include\_path, then these settings are not necessary. However, it is
|
|
|
124 |
> more efficient and (from experience) less error-prone to set them to
|
|
|
125 |
> absolute paths. This ensures that Smarty is getting files from the
|
|
|
126 |
> directories you intended.
|
|
|
127 |
|
|
|
128 |
Now, run your PHP file. You should see *\"Hello Ned, welcome to Smarty!\"*
|
|
|
129 |
|
|
|
130 |
You have completed the basic setup for Smarty!
|
|
|
131 |
|
|
|
132 |
## Extended Setup {#installing.smarty.extended}
|
|
|
133 |
==============
|
|
|
134 |
|
|
|
135 |
This is a continuation of the [basic
|
|
|
136 |
installation](#installing.smarty.basic), please read that first!
|
|
|
137 |
|
|
|
138 |
A slightly more flexible way to setup Smarty is to extend the Smarty
|
|
|
139 |
class and initialize your Smarty
|
|
|
140 |
environment. So instead of repeatedly setting directory paths, assigning
|
|
|
141 |
the same vars, etc., we can do that in one place.
|
|
|
142 |
|
|
|
143 |
```php
|
|
|
144 |
<?php
|
|
|
145 |
|
|
|
146 |
class Smarty_GuestBook extends Smarty {
|
|
|
147 |
|
|
|
148 |
public function __construct()
|
|
|
149 |
{
|
|
|
150 |
parent::__construct();
|
|
|
151 |
|
|
|
152 |
$this->setTemplateDir('/web/www.example.com/guestbook/templates/');
|
|
|
153 |
$this->setCompileDir('/web/www.example.com/guestbook/templates_c/');
|
|
|
154 |
$this->setConfigDir('/web/www.example.com/guestbook/configs/');
|
|
|
155 |
$this->setCacheDir('/web/www.example.com/guestbook/cache/');
|
|
|
156 |
|
|
|
157 |
$this->caching = Smarty::CACHING_LIFETIME_CURRENT;
|
|
|
158 |
$this->assign('app_name', 'Guest Book');
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
}
|
|
|
162 |
```
|
|
|
163 |
|
|
|
164 |
Now, we can use `Smarty_GuestBook` instead of `Smarty` in our scripts:
|
|
|
165 |
```php
|
|
|
166 |
$smarty = new Smarty_GuestBook();
|
|
|
167 |
$smarty->assign('name','Ned');
|
|
|
168 |
$smarty->display('index.tpl');
|
|
|
169 |
```
|