]> git.andy128k.dev Git - ipf-twig.git/commitdiff
initial commit
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 8 Feb 2015 10:16:24 +0000 (12:16 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 8 Feb 2015 10:32:39 +0000 (12:32 +0200)
.gitignore [new file with mode: 0644]
composer.json [new file with mode: 0644]
composer.lock [new file with mode: 0644]
src/twig.php [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..4acdc48
--- /dev/null
@@ -0,0 +1,3 @@
+.DS_Store
+/vendor
+
diff --git a/composer.json b/composer.json
new file mode 100644 (file)
index 0000000..dca8d2c
--- /dev/null
@@ -0,0 +1,26 @@
+{
+  "name": "andy128k/ipf-twig",
+  "authors": [
+    {
+      "name": "Andrey Kutejko",
+      "email": "andy128k@gmail.com"
+    }
+  ],
+  "repositories": [
+    {
+      "type": "composer",
+      "url": "http://packages.andy128k.net/php/"
+    }
+  ],
+  "autoload": {
+    "classmap" : ["src"]
+  },
+  "require": {
+    "twig/twig": "~1"
+  },
+  "suggest": {
+    "andy128k/ipf": "Web framework"
+  },
+  "minimum-stability": "dev"
+}
+
diff --git a/composer.lock b/composer.lock
new file mode 100644 (file)
index 0000000..54c32e3
--- /dev/null
@@ -0,0 +1,75 @@
+{
+    "_readme": [
+        "This file locks the dependencies of your project to a known state",
+        "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
+        "This file is @generated automatically"
+    ],
+    "hash": "789a2d96bafbb8d8ff7786cde5839e08",
+    "packages": [
+        {
+            "name": "twig/twig",
+            "version": "dev-master",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/twigphp/Twig.git",
+                "reference": "74a8bddec4e8fbea8b3f497bc7cc4bdf1a498330"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/twigphp/Twig/zipball/74a8bddec4e8fbea8b3f497bc7cc4bdf1a498330",
+                "reference": "74a8bddec4e8fbea8b3f497bc7cc4bdf1a498330",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.2.7"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.18-dev"
+                }
+            },
+            "autoload": {
+                "psr-0": {
+                    "Twig_": "lib/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "BSD-3-Clause"
+            ],
+            "authors": [
+                {
+                    "name": "Fabien Potencier",
+                    "email": "fabien@symfony.com",
+                    "homepage": "http://fabien.potencier.org",
+                    "role": "Lead Developer"
+                },
+                {
+                    "name": "Armin Ronacher",
+                    "email": "armin.ronacher@active-4.com",
+                    "role": "Project Founder"
+                },
+                {
+                    "name": "Twig Team",
+                    "homepage": "http://twig.sensiolabs.org/contributors",
+                    "role": "Contributors"
+                }
+            ],
+            "description": "Twig, the flexible, fast, and secure template language for PHP",
+            "homepage": "http://twig.sensiolabs.org",
+            "keywords": [
+                "templating"
+            ],
+            "time": "2015-01-30 11:04:23"
+        }
+    ],
+    "packages-dev": [],
+    "aliases": [],
+    "minimum-stability": "dev",
+    "stability-flags": [],
+    "prefer-stable": false,
+    "prefer-lowest": false,
+    "platform": [],
+    "platform-dev": []
+}
diff --git a/src/twig.php b/src/twig.php
new file mode 100644 (file)
index 0000000..e0bf95b
--- /dev/null
@@ -0,0 +1,49 @@
+<?php
+
+final class IPF_Twig
+{
+    public static function RenderToResponse($tplfile, $params=array(), $request=null)
+    {
+        return new IPF_HTTP_Response(self::RenderToString($tplfile, $params, $request));
+    }
+
+    public static function RenderToString($tplfile, $params=array(), $request=null)
+    {
+        $context = IPF_Project_Template::context($params, $request);
+        $twig = self::createEnvironment();
+        $template = $twig->loadTemplate($tplfile);
+        return $template->render($context);
+    }
+
+    private static function createEnvironment()
+    {
+        $options = array(
+            'cache' => IPF::get('tmp'),
+        );
+        if (IPF::get('debug')) {
+            $options['debug'] = true;
+            $options['auto_reload'] = true;
+        }
+
+        $twig = new Twig_Environment(self::createLoader(), $options);
+
+        $twig->addFunction(new Twig_SimpleFunction('url', function() {
+            return IPF_Project_Template::urlTag(func_get_args());
+        }));
+        $twig->addFunction(new Twig_SimpleFunction('params', function() {
+            return IPF_Project_Template::paramsTag(func_get_args());
+        }));
+
+        return $twig;
+    }
+
+    private static function createLoader()
+    {
+        $loader = new Twig_Loader_Filesystem(array());
+        foreach (IPF_Project_Template::templateDirs() as $dir) {
+            $loader->addPath($dir);
+        }
+        return $loader;
+    }
+}
+