Last active
August 29, 2015 14:07
-
-
Save VaclavSir/87fde7af7f0a49cabc02 to your computer and use it in GitHub Desktop.
{{ dump_array({'foo': 'bar'}) }}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AcmeDemoExtension extends \Twig_Extension { | |
public function getFunctions() { | |
return [ | |
'dump_array' => new \Twig_Function_Method($this, 'dumpArray'), | |
]; | |
} | |
public function dumpArray($arrayLikeObject, $indentLevel = 1) { | |
$dump = "[\n"; | |
$indentation = ' '; | |
foreach ($arrayLikeObject as $key => $value) { | |
if (is_scalar($value)) { | |
$value = '\'' . addcslashes($value, '\\\'') . '\''; | |
} else { | |
$value = $this->dumpArray($value, $indentLevel + 1); | |
} | |
$dump .= str_repeat($indentation, $indentLevel) . "'$key' => $value,\n"; | |
} | |
$dump .= str_repeat($indentation, $indentLevel - 1) . ']'; | |
return $dump; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment