Skip to content

Instantly share code, notes, and snippets.

@arnaud-lb
Created September 19, 2012 21:10
Show Gist options
  • Save arnaud-lb/3752280 to your computer and use it in GitHub Desktop.
Save arnaud-lb/3752280 to your computer and use it in GitHub Desktop.
Twig REPL
<?php
/**
* Twig REPL hack
*
* Arnaud Le Blanc <[email protected]>
*/
require '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 "$ ";
}
@arnaud-lb
Copy link
Author

Example:

$ php twig-repl.php
$ 512*2
1024
$ 512*2|number_format()
1024
$ (512*2)|number_format()
1,024
$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment