2017 © Pedro Peláez
 

library frctl-twig

Alias-Loader for handling fractal @handle's in twig and callable handler for the accompanied node module

image

liip/frctl-twig

Alias-Loader for handling fractal @handle's in twig and callable handler for the accompanied node module

  • Friday, March 9, 2018
  • by lsmith
  • Repository
  • 6 Watchers
  • 2 Stars
  • 981 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 1 Versions
  • 92 % Grown

The README.md

About

frctl-twig is an adapter consisting of an NPM and a Composer package. It integrates the Twig PHP template engine into fractal., (*1)

Installation

Inside your fractal project add a composer package by adding a composer.json:, (*2)

{
  "name": "my/fractal-project",
  "type": "project",
  "require-dev": {
    "liip/frctl-twig": "dev-master"
  },
}

Run composer install., (*3)

Add a devDependencies to the fractal twig adapter into your package.json:, (*4)

"frctl-twig": "git+https://github.com/liip/frctl-twig.git#master"

Run npm install., (*5)

Adding Twig Extensions

Add any relevant composer packages to your composer.json., (*6)

For exampe run composer require twig/twig-extensions., (*7)

Then add a file php-twig/TwigExtensions.php to your fractal project with the following content:, (*8)

<?php

namespace Frctl;

class TwigExtensions
{
    static public function getExtensions()
    {
        return [
            # Add your extensions here, for example the twig-extension text extension
            # new \Twig_Extensions_Extension_Text(),
        ];
    }
}

Then add the following section to your fractal project composer.json:, (*9)

  "autoload": {
    "psr-4": {
      "Frctl\\": "php-twig/"
    }
  }

How to use Twig templates in another project

Add the composer package pointing to your fractal project into the composer project of this other project., (*10)

Adjust the file loader to be able to find the twig templates in the fractal project:, (*11)

class TwigFilesystemLoader extends BaseTwigFilesystemLoader
{
    /**
     * Should probably be set via a setter from configuration
     *
     * @var string
     */
    private $fractalPath = '/path/to/fractal/twig/templates';

    /**
     * @param string $name
     *
     * @return string
     */
    protected function findTemplate($name)
    {
        $fractalPath = $this->getFractalPath();
        if ($fractalPath && preg_match('/^@fractal-(.*)$/', $name, $templatePath)) {
            $fullFilePath = $fractalPath . '/' . $templatePath[1];

            return $fullFilePath;
        }

        ...
    }
}

Load all the extensions into your Twig_Environment instance:, (*12)

    if (class_exists('Frctl\TwigExtensions')) {
        $extensions = \Frctl\TwigExtensions::getExtensions();
        foreach ($extensions as $extension) {
            $twig->addExtension($extension);
        }
    }

strict_variables and debug config flags

By default, the strict_variables flag of twig is set to false and the debug flag is set to true. To change these variables, pass them in an optional config object with their desired values while configuring fractal:, (*13)

    const frctlTwig = require("frctl-twig");

    fractal.components.engine(frctlTwig({
        strict_variables: true, // Or false
        debug: false // Or true
    }));

    // Further setup...

Credits

The code is based on the work by Benjamin Milde: * https://github.com/LostKobrakai/twig * https://github.com/LostKobrakai/frctl-twig, (*14)

The Versions

09/03 2018

dev-master

9999999-dev

Alias-Loader for handling fractal @handle's in twig and callable handler for the accompanied node module

  Sources   Download

MIT

The Requires

 

by Lukas Kahwe Smith