Skip to content

Instantly share code, notes, and snippets.

@VaclavSir
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VaclavSir/87fde7af7f0a49cabc02 to your computer and use it in GitHub Desktop.
Save VaclavSir/87fde7af7f0a49cabc02 to your computer and use it in GitHub Desktop.
{{ dump_array({'foo': 'bar'}) }}
<?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