Настройка composer.json:
Код:
{ "require": { "twig/twig": "^3.0" } }
Установка:
Код:
php composer.phar install
Результат установки:
Код:
No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information. Loading composer repositories with package information Info from https://repo.packagist.org: [37;44m#StandWith [30;43mUkraine [0m Updating dependencies Lock file operations: 3 installs, 0 updates, 0 removals - Locking symfony/polyfill-ctype (v1.27.0) - Locking symfony/polyfill-mbstring (v1.27.0) - Locking twig/twig (v3.4.3) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 3 installs, 0 updates, 0 removals 0 [>---------------------------] 0 [>---------------------------] - Installing symfony/polyfill-mbstring (v1.27.0): Extracting archive - Installing symfony/polyfill-ctype (v1.27.0): Extracting archive - Installing twig/twig (v3.4.3): Extracting archive 0/3 [>---------------------------] 0% 2/3 [==================>---------] 66% 3/3 [============================] 100% Generating autoload files 3 packages you are using are looking for funding. Use the `composer fund` command to find out more!
Пример:
Шаблон main.html.twig:
Код:
<!DOCTYPE html> <html> <head> <title>{{ header }}</title> </head> <body> <p> {{ text1 }} {{ text2 }} </p> </body> </html>
Файл index.php:
Код:
<?php require __DIR__ . '/vendor/autoload.php'; use Twig\Environment; use Twig\Loader\FilesystemLoader; $loader = new FilesystemLoader(__DIR__ . '/templates'); $twig = new Environment($loader); echo $twig->render('main.html.twig', ['header' => 'Simple Example', 'text1' => 'Hello', 'text2' => 'World!']); ?>
Результат: