litphp/view-twig

Twig integration for lit

v0.9.0 2019-06-29 13:41 UTC

This package is auto-updated.

Last update: 2024-10-29 04:25:46 UTC


README

Scrutinizer Code Quality Code Coverage Build Status

Twig integration for lit

Usage

In a standard litphp/project:

  • add dependency & install
composer require litphp/view-twig
  • append configuration

Create a template dir in your project root, says template. Write your first template file templates/index.twig

Hello {{name}}!

Merge TwigView::configuration into your configuration.php.

$configuration += \Lit\View\Twig\TwigView::configuration(C::instance(\Twig\Loader\FilesystemLoader::class, [
    __DIR__ . '/templates',
]));
  • integration in action class

In src/BaseAction.php, use the trait TwigViewBuilderTrait

abstract class BaseAction extends BoltAbstractAction
{
    use \Lit\View\Twig\TwigViewBuilderTrait;

Change your src/HomeAction.php to render page use twig

class HomeAction extends BaseAction
{
    protected function main(): ResponseInterface
    {
        return $this->twig('index.twig')->render(['name' => 'twig']);
    }

That's all! Run your app by php -S 127.0.0.1:3080 public/index.php, and open http://127.0.0.1:3080/. You should see greetings from twig template "Hello twig!"