|
1 | 1 | # Cherry-Templater |
| 2 | +The Cherry-project Template wrapper |
| 3 | + |
| 4 | +[](https://github.com/ABGEO07/cherry-templater/blob/master/LICENSE) |
| 5 | + |
| 6 | +[](https://github.com/ABGEO07/cherry-templater/releases) |
| 7 | + |
| 8 | +[](https://packagist.org/packages/cherry-project/templater "Packagist Version") |
| 9 | + |
| 10 | +------------ |
| 11 | + |
| 12 | +## Including |
| 13 | +**Install from composer** `composer require cherry-project/templater` |
| 14 | + |
| 15 | +**Include Autoloader in your main file** (Ex.: index.php) |
| 16 | +```php |
| 17 | +require_once __DIR__ . '/vendor/autoload.php'; |
| 18 | +``` |
| 19 | + |
| 20 | +## Class Templater |
| 21 | +Import class |
| 22 | +```php |
| 23 | +use Cherry\Templating\Templater; |
| 24 | +``` |
| 25 | +Crete class new object |
| 26 | +```php |
| 27 | +$templateEngine = new Templater(PATH_TO_TEMPLATES); |
| 28 | +``` |
| 29 | + |
| 30 | +Where `PATH_TO_TEMPLATES` is path to your templates folder. Ex.: `__DIR__ . '/../examples/templates'` |
| 31 | + |
| 32 | +Create new template in you'r templates folder (Ex.: `index.templater.php`) which contains simple HTML |
| 33 | +Markup with PHP: |
| 34 | + |
| 35 | +```html |
| 36 | +<!doctype html> |
| 37 | +<html lang="en"> |
| 38 | +<head> |
| 39 | + <meta charset="UTF-8"> |
| 40 | + <title>Hello, World!</title> |
| 41 | +</head> |
| 42 | +<body> |
| 43 | + <h1>Hello, <?php echo $name; ?>!</h1> |
| 44 | +</body> |
| 45 | +</html> |
| 46 | +``` |
| 47 | + |
| 48 | +Then you can call `$templateEngine` objects `render` method with two arguments: |
| 49 | + |
| 50 | +- Name of rendering template |
| 51 | +- Arguments (PHP Variables) for template |
| 52 | + |
| 53 | +Arguments is simple PHP Array: |
| 54 | + |
| 55 | +```php |
| 56 | +$args = [ |
| 57 | + 'name' => 'Name 1', |
| 58 | + 'surname' => 'Surname 1' |
| 59 | +]; |
| 60 | +``` |
| 61 | + |
| 62 | +Our `index.templater.php` template contains only one PHP Variable `$name`, so we must pass it in `render` method: |
| 63 | + |
| 64 | +```php |
| 65 | +$response = $templateEngine->render('index.templater.php', [ |
| 66 | + 'name' => 'Temuri' |
| 67 | +]); |
| 68 | +``` |
| 69 | + |
| 70 | +After that `$response` Variable will contain Response object and we can print it: |
| 71 | + |
| 72 | +```php |
| 73 | +echo $response; |
| 74 | +``` |
| 75 | + |
| 76 | +In first argument of `render` method we can put full filename of template (`index.templater.php`) |
| 77 | +or only template name (name without file extension Ex.: `index`) |
2 | 78 |
|
3 | 79 | **2019 © Cherry-project** |
0 commit comments