Skip to content

Instantly share code, notes, and snippets.

@hlfbt
Forked from arnaud-lb/twig-repl.php
Last active September 21, 2016 15:11
Show Gist options
  • Save hlfbt/6781dbdd6e4945171722c3ed6728c806 to your computer and use it in GitHub Desktop.
Save hlfbt/6781dbdd6e4945171722c3ed6728c806 to your computer and use it in GitHub Desktop.
Twig REPL
<?php
/**
* Twig REPL hack
*
* Arnaud Le Blanc <arnaud.lb@gmail.com>
*/
require 'vendor/twig/twig/lib/Twig/Autoloader.php';
Twig_Autoloader::register();
$loader = new Twig_Loader_Array(array());
$env = new Twig_Environment($loader);
$lineno = 0;
echo "$ ";
while (!feof(STDIN) && false !== $code = fgets(STDIN)) {
try {
$code = sprintf("{{ %s }}", trim($code));
$loader->setTemplate('command line code', $code);
$env->display('command line code');
echo "\n";
} catch (Exception $e) {
printf("Error executing %s:\n", $code);
printf("%s: %s\n", get_class($e), $e->getMessage());
}
echo "$ ";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment