Skip to content

Instantly share code, notes, and snippets.

@chrif
Created August 21, 2012 16:29
Show Gist options
  • Save chrif/3417057 to your computer and use it in GitHub Desktop.
Save chrif/3417057 to your computer and use it in GitHub Desktop.
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