Files

198 lines
5.7 KiB
Markdown
Raw Permalink Normal View History

2023-02-05 23:14:10 +01:00
# Getting started
## Requirements
2025-11-19 22:33:49 +01:00
Smarty can be run with PHP 7.2 to PHP 8.5.
## Installation
2023-02-05 23:14:10 +01:00
Smarty can be installed with [Composer](https://getcomposer.org/).
To get the latest stable version of Smarty use:
2023-02-05 23:14:10 +01:00
```shell
composer require smarty/smarty
2023-02-05 23:14:10 +01:00
```
To get the latest, unreleased version, use:
2023-02-05 23:14:10 +01:00
```shell
composer require smarty/smarty:dev-master
2023-02-05 23:14:10 +01:00
```
2023-11-29 13:30:13 +01:00
To get the previous stable version of Smarty, Smarty 4, use:
2023-02-05 23:14:10 +01:00
```shell
2023-11-29 13:30:13 +01:00
composer require smarty/smarty:^4
2023-02-05 23:14:10 +01:00
```
Here's how you create an instance of Smarty in your PHP scripts:
```php
<?php
2024-05-24 00:21:02 +02:00
// Instantiated via composer
require 'vendor/autoload.php';
2023-08-08 00:04:14 +02:00
use Smarty\Smarty;
$smarty = new Smarty();
2024-05-24 00:21:02 +02:00
// or ...
// Instantiated directly
require("/path/to/smarty/libs/Smarty.class.php");
use Smarty\Smarty;
$smarty = new Smarty();
```
2023-02-05 23:14:10 +01:00
Now that the library files are in place, it's time to set up the Smarty
directories for your application.
2023-08-08 00:04:14 +02:00
Smarty requires four directories which are by default named `templates`, `configs`, `templates_c` and `cache`
relative to the current working directory.
The defaults can be changed as follows:
2023-08-08 00:04:14 +02:00
```php
2023-08-08 00:04:14 +02:00
<?php
use Smarty\Smarty;
$smarty = new Smarty();
$smarty->setTemplateDir('/some/template/dir');
$smarty->setConfigDir('/some/config/dir');
$smarty->setCompileDir('/some/compile/dir');
$smarty->setCacheDir('/some/cache/dir');
```
The compile dir and cache dir need to be writable for the user running the PHP script.
2021-12-03 11:59:22 +01:00
> **Note**
>
> This is usually user "nobody" and group "nobody". For OS X users, the
> default is user "www" and group "www". If you are using Apache, you
> can look in your `httpd.conf` file to see what user and group are
> being used.
```bash
chown nobody:nobody /web/www.example.com/guestbook/templates_c/
chmod 770 /web/www.example.com/guestbook/templates_c/
2021-12-03 11:59:22 +01:00
chown nobody:nobody /web/www.example.com/guestbook/cache/
chmod 770 /web/www.example.com/guestbook/cache/
```
2021-12-03 11:59:22 +01:00
You can verify if your system has the correct access rights for
these directories with [`testInstall()`](./programmers/api-functions/api-test-install.md):
2021-12-03 11:59:22 +01:00
```php
2023-08-08 00:04:14 +02:00
<?php
use Smarty\Smarty;
$smarty = new Smarty();
$smarty->setTemplateDir('/some/template/dir');
$smarty->setConfigDir('/some/config/dir');
$smarty->setCompileDir('/some/compile/dir');
$smarty->setCacheDir('/some/cache/dir');
$smarty->testInstall();
```
## Basic usage
Now, let's create the `index.tpl` file that Smarty will display. This
needs to be located in the [`$template_dir`](./programmers/api-variables/variable-template-dir.md).
2023-02-05 23:14:10 +01:00
```smarty
{* Smarty *}
<h1>Hello {$name|escape}, welcome to Smarty!</h1>
```
2021-12-03 11:59:22 +01:00
> **Note**
>
> `{* Smarty *}` is a template [comment](./designers/language-basic-syntax/language-syntax-comments.md). It
2021-12-03 11:59:22 +01:00
> is not required, but it is good practice to start all your template
> files with this comment. It makes the file easy to recognize
> regardless of the file extension. For example, text editors could
> recognize the file and turn on special syntax highlighting.
Now lets edit our php file. We'll create an instance of Smarty,
[`assign()`](./programmers/api-functions/api-assign.md) a template variable and
[`display()`](./programmers/api-functions/api-display.md) the `index.tpl` file.
2021-12-03 11:59:22 +01:00
```php
<?php
2021-12-03 11:59:22 +01:00
require 'vendor/autoload.php';
2021-12-03 11:59:22 +01:00
2023-08-08 00:04:14 +02:00
use Smarty\Smarty;
$smarty = new Smarty();
2021-12-03 11:59:22 +01:00
$smarty->setTemplateDir('/web/www.example.com/guestbook/templates/');
$smarty->setCompileDir('/web/www.example.com/guestbook/templates_c/');
$smarty->setConfigDir('/web/www.example.com/guestbook/configs/');
$smarty->setCacheDir('/web/www.example.com/guestbook/cache/');
2021-12-03 11:59:22 +01:00
$smarty->assign('name', 'Ned');
$smarty->display('index.tpl');
2021-12-03 11:59:22 +01:00
```
2021-12-03 11:59:22 +01:00
> **Note**
>
2023-02-05 23:14:10 +01:00
> In our example, we are setting absolute paths to all the Smarty
2021-12-03 11:59:22 +01:00
> directories. If `/web/www.example.com/guestbook/` is within your PHP
> include\_path, then these settings are not necessary. However, it is
> more efficient and (from experience) less error-prone to set them to
> absolute paths. This ensures that Smarty is getting files from the
> directories you intended.
2023-02-05 23:14:10 +01:00
Now, run your PHP file. You should see *"Hello Ned, welcome to Smarty!"*
2021-12-03 11:59:22 +01:00
You have completed the basic setup for Smarty!
## Escaping
You may have noticed that the example template above renders the `$name` variable using
the [escape modifier](./designers/language-modifiers/language-modifier-escape.md). This
modifier makes string 'safe' to use in the context of an HTML page.
If you are primarily using Smarty for HTML-pages, it is recommended to enable automatic
escaping. This way, you don't have to add `|escape` to every variable you use on a web page.
Smarty will handle it automatically for you!
Enable auto-escaping for HTML as follows:
```php
$smarty->setEscapeHtml(true);
```
2023-02-05 23:14:10 +01:00
## Extended Setup
2021-12-03 11:59:22 +01:00
2023-02-05 23:14:10 +01:00
This is a continuation of the [basic installation](#installation), please read that first!
2021-12-03 11:59:22 +01:00
2023-02-05 23:14:10 +01:00
A slightly more flexible way to set up Smarty is to extend the Smarty
class and initialize your Smarty
2021-12-03 11:59:22 +01:00
environment. So instead of repeatedly setting directory paths, assigning
the same vars, etc., we can do that in one place.
```php
<?php
2021-12-03 11:59:22 +01:00
2023-08-08 00:04:14 +02:00
use Smarty\Smarty;
class My_GuestBook extends Smarty {
2021-12-03 11:59:22 +01:00
public function __construct()
{
parent::__construct();
2021-12-03 11:59:22 +01:00
$this->setTemplateDir('/web/www.example.com/guestbook/templates/');
$this->setCompileDir('/web/www.example.com/guestbook/templates_c/');
$this->setConfigDir('/web/www.example.com/guestbook/configs/');
$this->setCacheDir('/web/www.example.com/guestbook/cache/');
$this->setEscapeHtml(true);
2021-12-03 11:59:22 +01:00
$this->caching = Smarty::CACHING_LIFETIME_CURRENT;
$this->assign('app_name', 'Guest Book');
}
2021-12-03 11:59:22 +01:00
}
```
2021-12-03 11:59:22 +01:00
2023-08-08 00:04:14 +02:00
Now, we can use `My_GuestBook` instead of `Smarty` in our scripts:
```php
2023-02-05 23:14:10 +01:00
<?php
2023-09-13 22:15:06 +02:00
$smarty = new My_GuestBook();
2023-02-05 23:14:10 +01:00
$smarty->assign('name', 'Ned');
$smarty->display('index.tpl');
```