From 16d117a5e2b43854c18cc5a607b43bcb6f2dcf27 Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Wed, 17 Jan 2018 20:58:12 +0100 Subject: [PATCH] Initial Commit --- .gitignore | 2 + README.md | 23 ++++++++++++ composer.json | 26 +++++++++++++ phpunit.xml | 12 ++++++ src/Environment.php | 74 +++++++++++++++++++++++++++++++++++++ tests/EnvironmentTest.php | 17 +++++++++ tests/autoload.php | 3 ++ tests/expected.html | 11 ++++++ tests/res/base.html.twig | 9 +++++ tests/res/pluginA.html.twig | 4 ++ tests/res/pluginB.html.twig | 8 ++++ 11 files changed, 189 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 phpunit.xml create mode 100644 src/Environment.php create mode 100644 tests/EnvironmentTest.php create mode 100644 tests/autoload.php create mode 100644 tests/expected.html create mode 100644 tests/res/base.html.twig create mode 100644 tests/res/pluginA.html.twig create mode 100644 tests/res/pluginB.html.twig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..55940e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +composer.lock \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c2fdf32 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +# Twig Compose Environment +A **quick and dirty** solution for multiple sub templates in twig + +## Installation +```bash +$ composer require devtronic/twig-compose +``` + +## Usage + +Every sub template **must** extend from the base template +```php +compose('base.html.twig', ['pluginA.html.twig', 'pluginB.html.twig']); // Take a look in tests/res + +echo $template->render([ + // Your template data +]); + +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..b676d7d --- /dev/null +++ b/composer.json @@ -0,0 +1,26 @@ +{ + "name": "devtronic/twig-compose", + "description": "Compose twig templates from multiple child templates", + "type": "library", + "require": { + "php": "5.6 || ^7.0", + "twig/twig": "1.* || 2.*" + }, + "require-dev": { + "phpunit/phpunit": "^5.7" + }, + "license": "MIT", + "authors": [ + { + "name": "Julian Finkler", + "email": "julian@developer-heaven.de" + } + ], + "minimum-stability": "stable", + "autoload": { + "psr-4": { + "Devtronic\\TwigCompose\\": "src/", + "Devtronic\\Tests\\TwigCompose\\": "tests/" + } + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..cd8c517 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,12 @@ + + + + ./tests/ + + + + + ./src + + + diff --git a/src/Environment.php b/src/Environment.php new file mode 100644 index 0000000..6d88ac3 --- /dev/null +++ b/src/Environment.php @@ -0,0 +1,74 @@ +getExactPath($baseTemplate); + $templates = [$baseTemplate => file_get_contents($baseTemplatePath)]; + foreach ($subTemplates as $template) { + $templates[md5($template . uniqid()) . '.html.twig'] = file_get_contents($this->getExactPath($template)); + } + + $i = 0; + $prevKey = null; + foreach ($templates as $hash => $template) { + if ($i < 2) { + $i++; + $prevKey = $hash; + continue; + } + + if (strstr($template, $baseTemplatePath)) { + $template = str_replace($baseTemplatePath, $prevKey, $template); + } + + if (strstr($template, $baseTemplate)) { + $template = str_replace($baseTemplate, $prevKey, $template); + } + $templates[$hash] = $template; + $prevKey = $hash; + } + end($templates); + $newTemplate = key($templates); + + $loader = new Twig_Loader_Array($templates); + $originalLoader = $this->getLoader(); + $this->setLoader($loader); + $templateWrapper = $this->load($newTemplate); + $this->setLoader($originalLoader); + + return $templateWrapper; + } + + /** + * Gets the exact template path + * + * @param string $name The template name + * @return string The template path + * + * @throws \Twig_Error_Loader + * @throws \Twig_Error_Syntax + */ + private function getExactPath($name) + { + return $this->resolveTemplate($name)->getSourceContext()->getPath(); + } +} diff --git a/tests/EnvironmentTest.php b/tests/EnvironmentTest.php new file mode 100644 index 0000000..9f45965 --- /dev/null +++ b/tests/EnvironmentTest.php @@ -0,0 +1,17 @@ +compose('base.html.twig', ['pluginA.html.twig', 'pluginB.html.twig']); + $this->assertSame(file_get_contents(__DIR__ . '/expected.html'), $template->render(['what' => 'World'])); + } +} \ No newline at end of file diff --git a/tests/autoload.php b/tests/autoload.php new file mode 100644 index 0000000..c55fb89 --- /dev/null +++ b/tests/autoload.php @@ -0,0 +1,3 @@ + + + + Great title + + + + +

Hello World

+ + \ No newline at end of file diff --git a/tests/res/base.html.twig b/tests/res/base.html.twig new file mode 100644 index 0000000..da8e521 --- /dev/null +++ b/tests/res/base.html.twig @@ -0,0 +1,9 @@ + + + + {% block head '' %} + + + {% block body '' %} + + \ No newline at end of file diff --git a/tests/res/pluginA.html.twig b/tests/res/pluginA.html.twig new file mode 100644 index 0000000..3791784 --- /dev/null +++ b/tests/res/pluginA.html.twig @@ -0,0 +1,4 @@ +{% extends 'base.html.twig' %} +{% block head %} + {{ parent() }}Great title +{% endblock %} \ No newline at end of file diff --git a/tests/res/pluginB.html.twig b/tests/res/pluginB.html.twig new file mode 100644 index 0000000..5b8d2db --- /dev/null +++ b/tests/res/pluginB.html.twig @@ -0,0 +1,8 @@ +{% extends 'base.html.twig' %} +{% block head %} + {{ parent() }} + +{% endblock %} +{% block body %} +

Hello {{ what }}

+{% endblock %} \ No newline at end of file