Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

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