Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set a breakpoint in Twig templates and debug some or all variables ( equivalent to {{ dump() }} )
services:
twig_extension.inspect:
class: Acme\HelloBundle\Extensions\InspectExtension
tags:
- { name: twig.extension }
<?php
namespace Acme\HelloBundle\Extensions;
/**
* Inspect Twig templates with a debugger.
* Usages:
* {{ inspect() }}
* {{ inspect(myVar) }}
*/
class InspectExtension extends \Twig_Extension
{
/**
* @return array
*/
public function getFunctions()
{
return array(
'inspect' => new \Twig_Function_Method($this, 'inspect', array('needs_context' => true))
);
}
/**
* @param $context
*/
public function inspect($context)
{
$this->breakPoint(1 === func_num_args() ? $context : func_get_arg(1));
}
/**
* This where you set your breakpoint
*/
protected function breakPoint($twig)
{
return; // breakpoint
}
/**
* @return string
*/
public function getName()
{
return 'inspect';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment