liip/frctl-twig

Name: frctl-twig

Owner: Liip

Description: frctl-twig is an PHP Twig adapter for Fractal consisting of an NPM and a Composer package.

Created: 2018-03-05 14:20:24.0

Updated: 2018-03-19 20:39:59.0

Pushed: 2018-03-09 18:58:17.0

Homepage: https://liip.ch

Size: 15

Language: PHP

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

About

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

Installation

Inside your fractal project add a composer package by adding a composer.json:


ame": "my/fractal-project",
ype": "project",
equire-dev": {
"liip/frctl-twig": "dev-master"


Run composer install.

Add a devDependencies to the fractal twig adapter into your package.json:

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

Run npm install.

Adding Twig Extensions

Add any relevant composer packages to your composer.json.

For exampe run composer require twig/twig-extensions.

Then add a file php-twig/TwigExtensions.php to your fractal project with the following content:

p

space Frctl;

s 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:

utoload": {
"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.

Adjust the file loader to be able to find the twig templates in the fractal project:

s 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:

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

Credits

The code is based on the work by Benjamin Milde:


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.